Friday, November 2, 2012

Re: preg_match Warning on Email

instead of explode I would use Cake's String method:

$emails = String::tokenize($emails[0]);


Am Freitag, 2. November 2012 15:47:54 UTC+1 schrieb chetan varshney:
Now I assume you are taking input as comma separated multiple emails. If yes
use code like.

In Model

"email" => array(
'mustNotEmpty'=>array(
    'rule' => 'notEmpty',
    'message'=> 'Please enter to email(s)',
    'last'=>true),
'mustBeEmail'=> array(
    'rule' => 'validateEmails',
    'message'=> '',
    'last' => true)),

and put following function in same model

public function validateEmails($check) {
    $emails = array_values($check);
    $key = array_keys($check);
    $emails = explode(',', $emails[0]);
    foreach($emails as $email) {
        $email = trim($email);
        if(!empty($email)) {
            $valid = Validation::email($email);
            if(!$valid) {
                $this->validationErrors[$key[0]][0]= 'You have an error near '.$email;
                break;
            }
        }
    }
    return true;
}


nothing to do in controller. only call validation method from controller.

good luck

On Fri, Nov 2, 2012 at 8:05 PM, Chris <chri...@yahoo.com> wrote:
thank you for your reply chetan,... 
I'm steel getting an error: 
Warning (2): preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [APP/controllers/invitations_controller.php, line 83]

this is my invitations model: 

    var $validate = array(
     'user_id' => array(
      'rule' => 'notEmpty',
      'message' => 'user must be valid',
     ),
'email'=> array(
                    'mustNotEmpty'=>array(
                        'rule' => 'notEmpty',
                        'message'=> 'Please enter email',
                        'last'=>true),
                    'mustBeEmail'=> array(
                        'rule' => array('email'),
                        'message' => 'Please enter valid email',
                        'last'=>true)                  
                    ),

 );

and this is what I have in a invitations controller: 
        $emails = array();
        
        if(!empty($this->data['Invitation']['emails']))
        {
          $raw_emails = explode(',', $this->data['Invitation']['emails']);
          foreach($raw_emails as $email)
          {
            $email = trim($email);
            if(preg_match('VALID_EMAIL', $email)) // This is where I'm getting an error... 
              $emails[] = $email;
          }

debug($email);

I placed the debug in,... and I'm reading test email in the view,... but steel getting an error 



On Friday, November 2, 2012 6:52:42 AM UTC-7, chetan varshney wrote:
For email validation you should use-

'email'=> array(
                    'mustNotEmpty'=>array(
                        'rule' => 'notEmpty',
                        'message'=> __('Please enter email'),
                        'last'=>true),
                    'mustBeEmail'=> array(
                        'rule' => array('email'),
                        'message' => __('Please enter valid email'),
                        'last'=>true)                 
                    ),

and for deep checking

'email'=> array(
                    'mustNotEmpty'=>array(
                        'rule' => 'notEmpty',
                        'message'=> __('Please enter email'),
                        'last'=>true),
                    'mustBeEmail'=> array(
                        'rule' => array('email', true),
                        'message' => __('Please enter valid email'),
                        'last'=>true)                 
                    ),



On Fri, Nov 2, 2012 at 7:05 PM, Chris <chri...@yahoo.com> wrote:
hi guys,... I'm getting this preg_match Warning on Email issue no matter what I do,.... 
can anyone help me please,.... 

Warning (2): preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [APP/controllers/invitations_controller.php, line 83]

and this is what I have in a Model: 

    var $validate = array(
      'email' => array(
      'rule' => 'alphaNumeric',
      'required' => true,
      'allowEmpty' => false,
     ),
 );

thanks in advance,... 
chris

--
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.



--
Chetan Varshney
Ektanjali Softwares Pvt Ltd

--
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.
 
 



--
Chetan Varshney
Ektanjali Softwares Pvt Ltd

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