Controller validation in Cakephp

10,802

Solution 1

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

Solution 2

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

Solution 3

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 :)

Share:
10,802
Sankalp
Author by

Sankalp

Updated on June 04, 2022

Comments

  • Sankalp
    Sankalp almost 2 years

    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