Sankalp
Sankalp

Reputation: 1350

Controller validation in Cakephp

I wish to validate in controller in cakephp. Though my validations are working well in Models but instead of model I wish to validate it in controller as well.

What I did to validate in contrller.

  $validates = array('email' => array(
                    'required' => array(
                        'rule' => array('notEmpty'),
                        'message' => 'A email is required'
                    ),
                    'isUnique' => array(
                        'rule' => array('notEmpty'),
                        'message' => 'This email is already registered'
                    ),
                    'email' => array(
                        'rule' => array('email'),
                        'message' => 'Enter valid mail address'
                    )
            ));
            if ($this->User->validates($validates)) {
                die("Action can be performed as validated !! Fields are correct");
            } else {
                die("Action can't be performed  !! Fields are in-correct");
            }

It always end me in correct condition no matters if field is correct or not. Please help

Upvotes: 3

Views: 11645

Answers (3)

Chinmay235
Chinmay235

Reputation: 3414

Try this -

$data = $this->request->data;
$this->ModelName->set($data);

if ($this->ModelName->validates()) {
    // it validated logic
} else {
    // didn't validate logic
    $errors = $this->ModelName->validationErrors;
}

Suppose you want to validate a particular field in cakephp Controller, then for that below code will be use -

$this->ModelName->validationErrors['html_field_name'][] = 'Your Error Message goes here';

Upvotes: 2

HelloSpeakman
HelloSpeakman

Reputation: 830

Setting $this->Model->validates = $validates; will work for you as suggested in the previous answer but you risk overwriting all other validation rules which may be set in the Model. It's much better to add, modify and remove validation rules on the fly like such:

$this->Model->validator()
    ->add('email', 'required', array(
        'rule' => array('notEmpty'),
        'message' => 'A email is required'
    ))
    ->add('email', 'isUnique', array(
        'rule' => array('notEmpty'),
        'message' => 'This email is already registered'
    ))
    ->add('email', 'email', array(
        'rule' => array('email'),
        'message' => 'Enter valid mail address'
    ));

I left your array exactly as you presented it, however I assume you have the wrong rule on isUnique

You can read more about binding rules here: http://book.cakephp.org/2.0/en/models/data-validation.html#dynamically-change-validation-rules

Upvotes: 7

MooN
MooN

Reputation: 804

Edit your code:

$this->$Model->validate = array('email' => array(
                    'required' => array(
                        'rule' => array('notEmpty'),
                        'message' => 'A email is required'
                    ),
                    'isUnique' => array(
                        'rule' => array('notEmpty'),
                        'message' => 'This email is already registered'
                    ),
                    'email' => array(
                        'rule' => array('email'),
                        'message' => 'Enter valid mail address'
                    )
            ));

It work with me :)

Upvotes: 1

Related Questions