Issue
I'm having trouble with form validation under Cakephp 3.6
On old version of CakePHP validation was not a problem, but there is no way to validate my form by the model. I followed the tutorials of different sites, to look on the forums, any solution did not work. If I remember correctly, it seems to me that on the first version of CakePHP 3, the validation took place at the time of the save, there even if no data is entered, I have the save which takes place without any value. ..
My view:
//src/Template/Users/Register.ctp
<div class="wrapper">
<div id="register-box">
<div class="form-register">
<h2 class="title">Inscription</h2>
<?php
echo $this->Form->create();
echo $this->Form->control('email',[
'label' => false,
'placeholder' => 'Adresse email',
'class' => 'form-control',
//'required' => true
]);
echo $this->Form->control('username',[
'label' => false,
'placeholder' => 'Nom d\'utilisateur',
'class' => 'form-control',
]);
echo '<div class="form-group">';
echo $this->Form->control('lastname',[
'label' => false,
'placeholder' => 'Nom',
'class' => 'form-control w-50',
]);
echo $this->Form->control('firstname',[
'label' => false,
'placeholder' => 'Prénom',
'class' => 'form-control w-50',
]);
echo '</div>';
echo $this->Form->control('password',[
'label' => false,
'placeholder' => 'Mot de passe',
'class' => 'form-control',
]);
echo $this->Form->select('type',
[
1 => 'Prestataire',
2 => 'Client'
],
[
'empty' => 'Quel type de compte ?',
'class' => 'form-control',
]);
echo $this->Form->button('Suivant', ['class' => 'btn btn-red']);
echo $this->Form->end();
?>
</div>
</div>
</div>
My controller:
//src/Controller/UsersController.php
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Validation\Validator;
class UsersController extends AppController {
public function register() {
$user = $this->Users->newEntity();
if($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if($this->Users->save($user)) {
echo "ok";
} else {
echo "KO";
}
}
$this->set('user', $user);
}
}
and my Model Table:
//src/Model/Table/UsersTable.php
<?php
use Cake\ORM\Table;
use Cake\Validation\Validator;
class UsersTable extends Table {
public function initialize(array $config) {
$this->addBehavior('Timestamp');
}
public function validationDefault(Validator $validator)
{
return $validator
->notEmpty('username', "Un nom d'utilisateur est nécessaire")
->notEmpty('password', 'Un mot de passe est nécessaire');
}
}
An idea of how to apply validation on a user's registration?
Thank you
Solution
you have to add the namespace to your UsersTable
<?php
namespace App\Model\Table; /// <<< add this line
use Cake\ORM\Table;
use Cake\Validation\Validator;
class UsersTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
}
public function validationDefault(Validator $validator)
{
return $validator
->notEmpty('username', "Un nom d'utilisateur est nécessaire")
->notEmpty('password', 'Un mot de passe est nécessaire');
}
}
Answered By - jbe
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.