2012-03-02 60 views
1

我得到以下警告2:CakePHP的驗證:的preg_match()的Delimeter不能是字母,數字或反斜線

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

在最後一行[IF($這個 - > MerryParent->驗證() )]的以下edit()函數的merry_parents_controller。

有人能告訴我我做錯了什麼嗎?我還在下面包含了MerryParent模型的$ validate數組。謝謝。

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 

回答

0

要使用自定義驗證的正則表達式,你應該這樣做:

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

的更多信息:

+0

我試過你的解決方案,但我仍然得到相同的警告。 – vaanipala 2012-03-04 23:56:30

+3

實際上這個正則表達式沒有什麼問題。這是因爲我沒有公開顯示某些功能。我現在修好了。 – vaanipala 2012-03-05 06:17:25

+0

感謝您添加最後的評論,vaanipala。我只是用自定義驗證功能碰到了同樣的事情。將其更改爲公共工作。 – brism 2012-08-07 22:48:25

相關問題