3

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

4
  • 4
    did you set the data first? $this->User->set($this->request->data); see manual Commented Oct 7, 2013 at 6:40
  • Ohh, I didn't... Thanks for correction. Commented Oct 7, 2013 at 7:04
  • 1
    Please show where you've seen/why you are passing validation rules to the method validates it's not expecting rules which is why it doesn't work. Commented Oct 7, 2013 at 8:15
  • Generate some controllers with the CakePHP bake console. They will provide example code for validating via methods in a controller. Note that the validation itself still takes place in your model. Commented Oct 7, 2013 at 9:59

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

4 Comments

any idea whether this works from inside a shell script? I also need to remove a validation rule on the fly, but it says validator() is undefined.
where is this function to be added in model?
@Kunal This can be used anywhere, if using from the model itself you may want to call $this->validator()-> ... otherwise if in the controller $this->ModelName->validator() You may use it beforeValidate for example, it depends.
@SDP It should work from the shell script, just make sure to load the model and then call it like normal. I haven't tested this however.
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';

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.