Hi ,
In my user register form I am using cakephp model validation. The validation
is working perfectly. Unfortunately the validation error messages are
not displaying on my form. Would you please help me on this?
The code I am using is as follows. Please advise me what I am missing here.
Model:
<?php namespace App\Model\Table; use Cake\ORM\Table; use Cake\Validation\Validator; class UsersTable extends Table { public function initialize(array $config) { $this->table('tp_users'); $this->primaryKey('user_id'); } public function validationDefault(Validator $validator) { $validator = new Validator(); $validator ->notEmpty('username', 'A username is required') ->add('username', 'unique', [ 'rule' => 'validateUnique', 'provider' => 'table', 'message' => 'Not unique' ]) ->notEmpty('email', 'A email is required') ->add('email', 'validFormat', [ 'rule' => 'email', 'message' => 'E-mail must be valid' ]) ->add('email', 'unique', [ 'rule' => 'validateUnique', 'provider' => 'table', 'message' => 'Not unique' ]) ->notEmpty('password', 'A password is required') ->lengthBetween('password', [6, 8]) ->add('profile_image', 'file', [ 'rule' => ['mimeType', ['image/jpeg', 'image/jpg', 'image/png']], 'message' => 'Only jpeg, jpg, and png image extension are allowed to upload..', ]); /* $errors = $validator->errors($this->request->data()); if (!empty($errors)) { // Send an email. } */ return $validator; } } Controller: -------------------------------------------- public function add($role = null) { $this->viewBuilder()->layout('customer_register'); // New in 3.1 $this->set(compact('role')); $user = $this->Users->newEntity($this->request->data); if ($this->request->is('post')) { //echo print_r($this->request->data); //die; if ($this->request->data['password'] == $this->request->data['conpassword']) { /* echo $this->request->data['password']; echo $this->request->data['conpassword']; die; */ //$user = $this->Users->patchEntity($user, $this->request->data); if ($this->Users->save($user)) { $this->Flash->success(__('The user has been saved.')); return $this->redirect(['action' => 'index']); } else { $this->Flash->error(__('Unable to add the user.')); return $this->redirect(['action' => 'add', $role]); } } else { /* echo $this->request->data['password']; echo $this->request->data['conpassword']; die; */ $this->Flash->error(__('Password and Confirm Password Mis Match')); return $this->redirect(['action' => 'add', $role]); } } $this->set('user', $user); } View: ---------------------------------------------<?php //echo $this->Flash->render(); ?>
<?php echo $this->Flash->render('auth'); ?>
<?= $this->Form->create($user) ?>
<label>Username</label>
<div class="input-group">
<?= $this->Form->input('username',array('type'=>'text','class'=>'form-control','placeholder'=>'Username','label'=>false,'required'=>true)) ?>
</div>
<label>Email</label>
<div class="input-group">
<?= $this->Form->input('email',array('type'=>'email','class'=>'form-control','placeholder'=>'Email','label'=>false,'required'=>true)) ?>
</div>
<label>Password</label>
<div class="input-group">
<?= $this->Form->input('password',array('class'=>'form-control','placeholder'=>'Password','type'=>'password','label'=>false,'required'=>true)) ?>
</div>
<label>Confirm Password</label>
<div class="input-group">
<?= $this->Form->input('conpassword',array('class'=>'form-control','placeholder'=>'Confirm Password','type'=>'password','label'=>false,'required'=>true)) ?>
</div>
<?= $this->Form->input('role_id',array('type'=>'hidden','label'=>false,'value'=>$role)) ?>
<?php //echo $this->Form->button(__('Submit'));
?>
<?php echo $this->Form->button('Submit', array('formnovalidate' => true));
?>
<?= $this->Form->end(); ?>
</div>