Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass validationErrors from controller to a view?

Well I have set validationErrors for login in my UsersController:

public function login() {
    if ($this->request->is('post')) {
        $this->User->set($this->request->data);
        if ($this->User->validates() && $this->Auth->login()) {
            $this->set('ui', $this->Auth->user('id'));
            $this->Session->setFlash(__('Loged in!'), 'flash_success');
            $this->redirect($this->Auth->redirect());
        } else {
            $errors = $this->User->validationErrors;
        }
    }

}

Now how can I use $error in my view or as an element to be listed above my form?

Plz help I have searched a lot, but the answers were for old CakePHP, and I am using CakePHP 2.3.8.

like image 491
Ash Avatar asked May 01 '26 15:05

Ash


1 Answers

Validation errors are available in the view automatically

There is no action required to get validation errors in the view, as they are a property of the view class. They can be inspected simply with:

debug($this->validationErrors);

In the view.

But you probably don't need to access them

Note however that it's not normal to need to look at this property directly. Using the form helper errors are displayed automatically, or you can generate errors individually

if ($this->Form->isFieldError('email')) {
    echo $this->Form->error('email');
}
like image 86
AD7six Avatar answered May 03 '26 13:05

AD7six



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!