开发者

CakePHP - Models (Custom E-mail validation)

开发者 https://www.devze.com 2023-04-13 07:27 出处:网络
I have the following custom function, that checks if an email has a gmail.com account ... function check_email($mail) {

I have the following custom function, that checks if an email has a gmail.com account ...

  function check_email($mail) {  
list($user, $domain) = explode('@', $mail);


if ($domain !== 'gmail.开发者_JAVA百科com' ){
return false;

} else{
    return true; 
    } }

On my Models, I want to check if it returned true, and if not, it would give an error message. This is what I got (a section of the 'var $validate = array ) :

'email' => array(
          'emailvalid' => array('check_email' => 'email',
      'message' => 'Not a valid email address') 
           )

I keep getting the error message even when even when I use a gmail.com account... What am I doing wrong?


It looks like you are missing the rule field in your $validate array. Also, the value passed to the validation method is actually an array, not a string. Check out the syntax for custom validation rules here.

Try something like this:

var $validate = array(
   'email' => array(
        'rule'    => array('checkEmail'),
        'message' => 'Not a valid email address.' 
    )
);

function checkEmail($value) {
    list($user, $domain) = explode('@', $value['email'], 2);
    return $domain === 'gmail.com';
}


did you try to use cake's great debugging tools? like pr() or debug() or a simple print_r()? then you would find such a simple mistake yourself in less then seconds.

in your case $mail will contain an array and is therefore not suitable for a direct comparison

so it should be more like

function check_email($data) { 
    $mail = array_shift($data);
    ....
}

also your validation array seems kind of strange to me. see the cookbook on how to correctly set up custom rules.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号