Ash
Ash

Reputation: 51

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.

Upvotes: 3

Views: 338

Answers (1)

AD7six
AD7six

Reputation: 66188

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');
}

Upvotes: 1

Related Questions