CakePHP 2.3 form that does not display validation errors

I tried to debug this for about two days and read every answer to this question on the Internet ... and still cannot get it to work.

In my CakePHP application, I have a user registration form that uses the User model and form assistant. When validation fails, the form does not display validation errors.

The $this->User->validationErrors array is correctly populated before the view is rendered according to debug() , and the fact that the model is not validated is determined by $this->User->save() .

Here is my view:

 <?php echo $this->Form->create('User'); echo $this->Form->input('soton_username', array('label' => 'Email address', 'after' => '@soton.ac.uk')); echo $this->Form->input('username', array('label' => 'Choose a username')); echo $this->Form->input('password'); echo $this->Form->input('password_confirm', array('label' => 'Password (again)', 'type' => 'password')); echo $this->Form->input('first_name'); echo $this->Form->input('last_name'); echo $this->Form->submit('Create account'); echo $this->Form->end(); ?> 

Here is my action:

 public function create() { if($this->request->is('post')) { $this->User->set($this->request->data); $valid = true; $validationErrors = array(); $password = $this->request->data('User.password'); $password_confirm = $this->request->data('User.password_confirm'); if(strlen($password) < 5) { $validationErrors['password'][] = 'Password must be at least 5 characters long'; $valid = false; } if($password != $password_confirm) { $validationErrors['password_confirm'][] ='Passwords do not match'; $valid = false; } $this->User->data['User']['email'] = $this->request->data('User.soton_username') . '@soton.ac.uk'; $group = $this->Group->find('first', array('conditions' => array('default' => 1))); if($group) { $gid = $group['Group']['id']; } else { $gid = 1; } $this->User->data['User']['group_id'] = $gid; if($this->User->validates() && $valid) { $this->User->save(); $this->Session->setFlash('<a href="/users/activate/' . $this->User->data['User']['activation_id'] . '">Activate</a>'); return $this->redirect('/users/confirm_create'); } $this->User->validationErrors = array_merge($this->User->validationErrors, $validationErrors); if(isset($this->User->validationErrors['email'])) { $this->User->validationErrors['soton_username'] = $this->User->validationErrors['email']; } //debug($this->request->validationErrors);exit; $this->Session->setFlash('There were errors with the form.'); } } 

The soton_username and password_confirm fields are not really part of the database, but they still need to be checked.

There is a $validate variable in my user model that the form recognizes when it inserts required="required" in the right places.

Any help would be appreciated.

+6
source share
2 answers

Ok, so I found the answer to my question.

The following function in my AppController class is culrprit:

 public function beforeRender() { $user_id = $this->Auth->user('id'); $user = $this->User->read(null, $user_id); $this->set('user', $user); } 

For some reason, calling the $this->Model->read() function prevents the display of validation errors, and this function called it at the end of each action. Moving it to the beforeFilter() function fixes the problem.

+5
source

Here is my answer as I did not call User-> read ():

I dug the cake core and found that the error fields are automatically populated based on the model name passed in with the form → create ('model').

My model is "User", and in my opinion I had $ this-> form-> create ("Users"). Pay attention to the 's'. Validation is performed on the model, so the array of validation errors is indexed by the model name: "User". The form uses the name passed to create, 'Users', to find errors.

To fix this, make sure you use a single model form in the form-> create (). For me, this is "User".

+1
source

Source: https://habr.com/ru/post/950029/


All Articles