Monday, February 23, 2015

Re: Handle validation rule message inside controller

http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html

First, set the data to the model:
$this->ModelName->set($this->request->data);

Then, to check if the data validates, use the validates method of the model, which will return true if it validates and false if it doesn't:
if ($this->ModelName->validates()) {
// it validated logic
} else {
// didn't validate logic
$errors = $this->ModelName->validationErrors;
}

Andras

> On Feb 21, 2015, at 12:50 AM, giuseppe giorgio <giozh89@gmail.com> wrote:
>
> Hi there, i've start to learn cake php yesterday, and i have a question. I've declared validation array inside my model class like:
>
>
> public $validate = array(
> 'username' => array(
> 'alphaNumeric' => array(
> 'rule' => 'alphaNumeric',
> 'required' => true,
> 'message' => 'username required'
> ),
> 'unique' => array(
> 'rule' => 'isUnique',
> 'required' => 'create',
> 'message' => 'Username already used'
> )
> ));
>
>
> When i add a new record to my database by using add(), everithing works fine, and alert message are showed automatically. How can i intercept this error message firing inside add() controller function? because if a message is fired, i would perform an action (and not simply show message)
>
> --
> Like Us on FaceBook https://www.facebook.com/CakePHP
> Find us on Twitter http://twitter.com/CakePHP
>
> ---
> You received this message because you are subscribed to the Google Groups "CakePHP" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
> To post to this group, send email to cake-php@googlegroups.com.
> Visit this group at http://groups.google.com/group/cake-php.
> For more options, visit https://groups.google.com/d/optout.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: