cakephp验证:preg_match():Delimeter不能是字母数字或反斜杠

时间:2012-03-02 09:12:52

标签: validation cakephp preg-match

我收到以下警告2:

preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash 

在merry_parents_controller的以下edit()函数的最后一行[if($ this-> MerryParent-> validates())]。

有人可以告诉我我做错了什么吗?我还在下面包含了$ validate数组的MerryParent模型。谢谢。

public function edit() {
    $user_id = $this->Auth->user('id'); //$this->Auth->user provides info abt the currently authenticated user
    $user=$this->MerryParent->getMerryParents($user_id);
    //since user doesn't enter username and password here, both the validations is unset to 
    //avoid getting validationErrors for var_dump($this->MerryParent->validationErrors).
    unset($this->MerryParent->validate['username']);
    unset($this->MerryParent->validate['password']); 
    //print_r($user_id);
    //print_r($user);

    // Form Processing
    if (!empty($this->data)) {
        $this->MerryParent->id = $user_id;
        $this->MerryParent->set($this->data); //before updating data must be set
                        var_dump($this->MerryParent->validationErrors);

        if ($this->MerryParent->validates()) {
                         ....

MerryParents模型

var $validate=array(
            'initial'=>array(
                'rule'=>'notEmpty',
                'message'=>'Please select your initial',
                'on'=>'create'
                ),
            'name'=>array(
                    'rule'=>array('minLength',3),
                    'required'=>true,
                    'allowEmpty'=>false,
                    'message'=>'Name is required!',
                    'on'=>'create'
                    ),
            'username'=>array(
                'userRule1'=>array(
                        'rule'=>array('minLength',3),
                        'required'=>true,
                        'allowEmpty'=>false,
                        'message'=>'Username is required!',
                        //'on'=> 'update'
                        ),
                 'userRule2'=>array(
                        'rule'=>'isUnique',
                        'message'=>'This username has already been taken!',
                        //'on'=>'update'
                        )
                   ),
            /*'email'=>array(                                   
                    'rule'=>array('email'),  
                    'required'=>true, 
                    'allowEmpty'=>false,
                    'message'=>'Valid email address required!'
                    ),*/
            'email' => array(
                'isUnique' => array(
                    'rule' => 'isUnique',
                    'message' => 'That email has already been taken',
                    'on' => 'create'
                ),
                'email' => array(
                    //'rule' => array('email', true),//boolean true as second parameter verifies that the host for the address is valid -- to be uncommented once website is uploaded
                    'rule' => array('email'),
                    'message' => 'Your email is invalid'
                ),
                'notEmpty' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Your email is required'
                )
            ),
            'password'=>array(
                'passwordRule1'=>array(
                        'rule'=>'isUnique',
                        'message'=>'This password has already been taken!',
                        'on'=>'update'
                        ),
                'passwordRule2'=>array(
                        'rule'=>'notEmpty',
                        'required'=>true,
                        'allowEmpty'=>false,
                        'message'=>'password required!',
                        'on'=>'update'
                        ),
                /*'isMatch' => array(
                        'rule' => array('isMatch', 'confirmPassword'),
                        'message' => 'The passwords did not match'
                        )*/
                ),
            'oldPassword'=>array(
                        'rule'=>'corretPassword',
                        'message'=>'Invalid password!'
                        ),
            'newPassword'=>array(
                        'rule'=>'isUnique',
                        'message'=>'This password has already been taken!'
                        ),      
            'confirmPassword'=>array(
                'confirmPasswordRule1'=>array(
                            'rule'=>'notEmpty',
                            //'required'=>true,
                            'allowEmpty'=>false,
                            'message'=>'confirm password required!'
                            ),
                'isMatch'=>array(
                            'rule'=>'isMatch',
                            'message'=>'The passwords did not match!'
                            )
            ),
            'landline'=>array(
                   'rule'=>array('custom','/(0[0-9]{2,4}-[2-9][0-9]{5,7})/'), 
                   'required'=>false, 
                   'allowEmpty'=>true, 
                   'message'=>'Invalid phone number! phone number format: eg 020-22345678 OR 0544-7573758 OR 02345-874567',
                   'on'=>'create'
                   ),
            'mobile'=>array(
                 'rule'=>array('custom','/([89]{1}[0-9]{9})/'), 
                 'required'=>true, 
                 'allowEmpty'=>false, 
                 'message'=>'Invalid mobile number! mobile number format: eg 9876543211',
                 'on'=>'create'
                 ),
            'address'=>array(
                 'rule'=>array('minLength',5),
                 'required'=>true, 
                 'allowEmpty'=>false, 
                 'message'=>'Please enter your address.',
                 'on'=>'create'
                 ),
        'state_id'=>array(
                'rule'=>'notEmpty',
                'message'=>'Please select your state',
                'on'=>'create'
                ),
        'city_id'=>array(
                'rule'=>'notEmpty',
                'required'=>true,
                'allowEmpty'=>false,
                'message'=>'Please select your city',
                'on'=>'create'
                ),
        'postal_code'=>array(
                     'rule'=>array('numeric',6),
                     'required'=>true, 
                     'allowEmpty'=>false,
                     'message'=>'valid postal code required!',
                     'on'=>'create'
                     )
            );//closing bracket for $validate array

1 个答案:

答案 0 :(得分:0)

要使用正则表达式进行自定义验证,您应该使用:

'rule' => '/([89]{1}[0-9]{9})/',

更多信息: