Saturday, January 12, 2013

Re: Conditional validation rule1 OR rule2

Hello Cricket,

I got this error message:
Error: Call to undefined method ModelValidator::url()

Please advise

On Saturday, January 12, 2013 3:11:26 AM UTC+2, cricket wrote:
You'll need to create your own validation method.
http://book.cakephp.org/2.0/en/models/data-validation.html#custom-validation-rules

Personally, I'd create columns for both url and email in the table,
then set up a custom rule to check that either one is there, and that
whichever one is there is correct. Something like this:

public $validate = array(
        'email' => array(
                'rule' => array('emailOrUrl', 'url'),
                'message' => 'Please supply a valid URL or email address'
        ),
        'url' => array(
                'rule' => array('emailOrUrl', 'email'),
                'message' => 'Please supply a valid URL or email address'
        )
);


function emailOrUrl($field = array(), $other_field = null)
{
        $key = key($field);
        
        /* If this is empty, the other shouldn't be
         */
        if (empty($field[$key]) && empty($this->data[$this-alias][$other_field]))
        {
                return false;
        }
        
        switch ($key)
        {
                case 'email':
                        return $this->validator()->email($field[[$key]);
                        
                case 'url':
                        return $this->validator()->url($field[[$key]);
                        
                default:
                        return false;
        }
}

Check the API for other params you can pass to these two methods.

http://api20.cakephp.org/class/validation


On Fri, Jan 11, 2013 at 2:49 PM, gonzela2006 <gonze...@gmail.com> wrote:
> Hello,
>
> I want to perform conditional validation between two rules
>
>     public $validate = array(
>         'variable' => array(
>             'notEmpty' => array(
>                 'rule' => 'notEmpty'
>             ),
>
>              'url' => array(
> 'rule' => array('url', true),
> 'message' => 'Please supply a valid URL address.'
> )
>
>      'email' => array(
>        'rule'    => array('email', true),
>        'message' => 'Please supply a valid email address.'
>    )
>
>         )
>     );
>
> I want to check if the "variable" is url or email
>
> Thanks,
> gonzela2006
>
> --
> 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 post to this group, send email to cake...@googlegroups.com.
> To unsubscribe from this group, send email to
> cake-php+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/cake-php?hl=en.
>
>

--
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 post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: