On Saturday, 12 January 2013 02:11:26 UTC+1, 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;
> }
> }
>
Youre better off not merging validation rules together like that rather:
public $validate = array(
'email' => array(
'emailOrUrl' => array(
'rule' => 'validateEmailOrUrl',
'message' => 'Email or url are required'
),
'email' => array(
'rule' => 'email',
'message' => 'Type a real email, yo'
)
),
'email' => array(
'emailOrUrl' => array(
'rule' => 'validateEmailOrUrl',
'message' => 'Email or url are required'
),
'email' => array(
'rule' => 'url',
'message' => 'Type a real url, yo'
)
)
);
public function validateEmailOrUrl() {
return !(empty($this->data[$this->alias]['email'] &&
empty($this->data[$this->alias]['url']));
}
Otherwise it's just another form of repetition.
AD
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
Visit this group at http://groups.google.com/group/cake-php?hl=en.