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.
In the code above, I always end in else
add.ctp
So it seems my identicalFields function is not called.
Any idea?
Alaini
-- 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"))); ?>
</div>
<div class="form-group">
<?= $this->Form->input('last_name', array('label' => __("Nom :"), 'class' => 'form-control', 'placeholder' => __("Nom"))); ?>
</div>
<div class="form-group">
<?= $this->Form->input('email', array('label' => __("email :"), 'class' => 'form-control', 'placeholder' => __("Email"))); ?>
</div>
<div class="form-group">
<?= $this->Form->input('phone', array('label' => __("Téléphone :"), 'class' => 'form-control', 'placeholder' => __("Téléphone"))); ?>
</div>
<div class="form-group">
<?= $this->Form->input('tva_umber', array('label' => __("Numéro de TVA :"), 'class' => 'form-control', 'placeholder' => __("Numéro de TVA"))); ?>
</div>
<div class="form-group">
<?= $this->Form->input('address', array('label' => __("Adresse :"), 'class' => 'form-control', 'placeholder' => __("Adresse"))); ?>
</div>
<div class="form-group">
<?= $this->Form->input('postcode', array('label' => __("Code postal :"), 'class' => 'form-control', 'placeholder' => __("Code postal"))); ?>
</div>
<div class="form-group">
<?= $this->Form->input('city', array('label' => __("Ville :"), 'class' => 'form-control', 'placeholder' => __("ville"))); ?>
</div>
</fieldset>
<?= $this->Form->button(__('Enregistrer')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
</div>
/**
* Users Model
*/
class UsersTable extends Table {
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config) {
$this->table('users');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('Owners', [
'foreignKey' => 'owner_id',
]);
$this->belongsTo('SitesUsers', [
'foreignKey' => 'sites_users_id',
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator) {
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create')
->validatePresence('username', 'create')
->notEmpty('username', __("Vous devez indiquer un nom d'utilisateur"))
->add('username', 'unique', ['rule' => 'validateUnique', 'provider' => 'table', 'message' => __("Ce nom d'utilisateur est déjà utilisé")])
->validatePresence('password', 'create')
->notEmpty('password', __("Vous devez indiquer un mot de passe"))
->validatePresence('password2', 'create')
->notEmpty('password2', __("Vous devez confirmer votre mot de passe"))
->add('password2', 'custom', ['rule' => 'identicalFields'])
->notEmpty('first_name', __("Vous devez indiquer votre prénom"))
->notEmpty('last_name', __("Vous devez indiquer votre nom"))
->allowEmpty('company')
->add('email', 'validFormat', ['rule' => 'email', 'message' => __("Cet email est déjà utilisé")])
->validatePresence('email', 'create')
->notEmpty('email')
->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table', 'message' => __('Cet email est déjà utilisé')])
->notEmpty('role')
->add('owner_id', 'valid', ['rule' => 'numeric'])
->allowEmpty('owner_id')
->allowEmpty('active')
->allowEmpty('token')
->add('sites_users_id', 'valid', ['rule' => 'numeric'])
->allowEmpty('sites_users_id', 'create');
return $validator;
}
public function identicalFields($value, $context) {
debug($value); debug($context); die();
}
}
So it seems my identicalFields function is not called.
Any idea?
Alaini
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 cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment