Here is how I did it in Cake 3:
In the User Entity,
make sure the $accessible list contains the confirm_password whether you
store it in the Db or not... and that you use the setPassword method to
hash your password when setting it.
protected $_accessible = [
'username' => true,
'password' => true,
'confirm_password' => true,
'role' => true,
'email' => true,];
protected function _setPassword($password) {
return (new DefaultPasswordHasher)->hash($password);
}
protected function _setConfirmPassword($password) {
return (new DefaultPasswordHasher)->hash($password);
}
In your Model usersTable:
add the following in your chain of validations in your validator:
->add('password','custom',['rule'=> function($value, $context){
if(isset($context->data['confirm_password']) && $value
!= $context->data['confirm_password']){
return false;
}
return true;
}, 'message'=>"Your password does not match your confirm
password. Please try again", 'on'=>
['create','update'],'allowEmpty'=>true])
Then as long as you create the form in your view containing both the
password and confirm_password, the validation will work.
On Friday, November 14, 2014 9:14:57 PM UTC+7, Alain Bonnefoy wrote:
>
> Hello,
>
> I tried to implement password confirmation on user registration in cakePHP
> 3 application.
>
> I did it in cakePHP 2 but it seems quite different here. It seems I can't
> get my validation function being called.
>
> In the form created below, username and password come form users table.
> password is just here for verification and all other fields come from
> owner table.
>
> /**
> * OwnersController/Add method
> *
> * @return void
> */
> public function add() {
> $this->loadModel('Users');
>
> $user = $this->Users->newEntity($this->request->data);
> $owner = $this->Owners->newEntity($this->request->data);
>
> if ($this->request->is('post')) {
>
> if ($this->Owners->validate($owner) && $this->Users->validate(
> $user)) {
> if ($this->Owners->save($owner)) {
> $user['owner_id'] = $owner['id'];
> $user['email'] = $owner['email'];
> $user['role'] = 'owner';
> $user['active'] = '1';
> $user['token'] = md5(time() . '-' . uniqid());
>
> if ($this->Users->save($user)) {
> $this->Flash->success(__("Merci de vous être
> enregistré. un email a été envoyé à {0} pour activer votre compte", $owner
> ['email']));
> return $this->redirect(['action' => 'index']);
> }
> }
> debug($owner); debug($user); die();
> $this->Flash->error(__("Impossible de vous enregistrer,
> veuillez corriger les erreurs"));
> } else {
> $this->Flash->error(__("Impossible de vous enregistrer,
> veuillez corriger les erreurs"));
> debug($owner); debug($user); debug($this->request->data);
> }
> }
> $this->set(compact('owner'));
> }
>
>
> In the code above, I always end in else
>
> add.ctp
> <div class="container-fluid">
> <div class="row">
> <div class="col-md-9">
> <h1><?= __("Création d'un nouveau propriétaire") ?></h1>
> <p> </p>
> <div class="col-md-19 col-md-offset-2 container-fluid well">
> <?= $this->Form->create($owner) ?>
> <fieldset>
> <div class="form-group">
> <?= $this->Form->input('username', array(
> 'label' => __("Nom d'utilisateur :"), 'class' => 'form-control',
> 'placeholder' => __("Nom d'utilisateur"))); ?>
> </div>
> <div class="form-group">
> <?= $this->Form->input('password', array(
> 'label' => __("Mot de passe :"), 'class' => 'form-control', 'placeholder'
> => __("Mot de passe"))); ?>
> </div>
> <div class="form-group">
> <?= $this->Form->input('password2', array(
> 'label' => __("Confirmation de mot de passe :"), 'type' => 'password',
> 'class' => 'form-control', 'placeholder' => __("Confirmation de mot de
> passe"))); ?>
> </div>
> <div class="form-group">
> <?= $this->Form->input('company', array(
> 'label' => __("Société :"), 'class' => 'form-control', 'placeholder' => __
> ("Société"))); ?>
> </div>
> <div class="form-group">
> <?= $this->Form->input('first_name', array(
> 'label' => __("Prénom :"), 'class' => 'form-control', 'placeholder' => __(
> "Prénom"))); ?>
> ...
--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.