2012-02-28 141 views
1

我們可以創建驗證規則,它可以作爲核心驗證規則工作。在cakephp中創建驗證規則,它可以用作核心驗證規則

假設我想用它可以使用輸入fields.Means我有這樣一個功能的驗證功能:

function checkinput($input) 
    { 
    $arr=array('x','y','z'); 
    if(in_array($input,$arr)) 
     return false; 
    else 
     return true; 
    } 

現在我想要使用此功能在所有型號的驗證。假設這個函數已經創建了一個自定義規則,名稱爲checkinput。 我想在任何模型使用此驗證:

var $validate = array(
    'name' => array(
     'notempty' => array(
      'rule' => 'notEmpty', 
      'message' => 'Please provide a name.', 
     ), 
     'checkinput' => array(
      'rule' => 'checkinput', 
      'message' => "You can't use X,Y,Z as name.", 
     ), 
    )); 

可這種自定義的驗證規則的行爲或通過其他方法來創建..

回答

0

這會幫助你: 「自定義的驗證規則」 http://www.dereuromark.de/2010/07/19/extended-core-validation-rules/

http://www.dereuromark.de/2011/10/07/maximum-power-for-your-validation-rules/ 的代碼在github上

請注意,您需要array_shift第一(詳見調試輸出):

function validateCustom($field) { 
    $field = array_shift($field); 
    .... 
} 
+0

似乎矯枉過正。 – Dave 2012-02-28 20:03:30

+0

究竟是什麼? ... – mark 2012-02-28 20:07:33

1

你應該能夠把該功能在你的app_model.php(或AppModel.php取決於你使用這將給所有模型訪問進行驗證/其他目的,其功能蛋糕的版本

引述手冊:。

在查找Validation類的方法 之前,首先檢查模型/行爲方法。這意味着您可以在應用程序級別 (,通過將該方法添加到AppModel)或模型級別覆蓋現有的 驗證方法(例如alphaNumeric())。

0

我們發佈了驗證規則。這個例子最適合在cakephp中進行驗證。

public $validate = array(
    'name' => array(
      'rule' => array('custom', '/^[a-z0-9]{1,}$/i'), 
      'message' => 'Alphabets and numbers only' 
     ), 
    'user_name' => array(
     'isUnique' => array(
      'rule' => 'isUnique', 
      'message' => 'The username has already been taken.', 
     ), 
     'notEmpty' => array(
      'rule' => array('custom','/^[a-z0-9]{1,}$/i'), 
      'message' => 'Alphabets and numbers only', 
     ), 
    ), 

    /*'password' => array(
     'rule' => array('minLength', 6), 
     'message' => 'Passwords must be at least 6 characters long.', 
    ),*/ 
    'password1' => array(
    'password' => array(
     'rule' => '/^[a-z0-9]{1,}$/i', 
     'message' => 'Only letters and integers' 
     ), 

     'between' => array(
      'rule' => array('between', 6, 50), 
      'message' => 'Between 6 to 50 characters' 
     ),), 
    'confirm_password' => array(
     'equaltofield' => array(
     'rule' => array('equaltofield','password1'), 
     'message' => 'Password does not match.', 
     //'allowEmpty' => false, 
     //'required' => false, 
     //'last' => false, // Stop validation after this rule 
     'on' => 'create', // Limit validation to 'create' or 'update' operations 
     ), 
     'compare' => array(
      'rule'  => array('validate_passwords'), 
      'message' => 'The passwords you entered do not match.', 
     ) 

    ), 
    'email_address' => array(
     'email' => array(
      'rule' => array('custom','/^[A-Za-z0-9._%+-][email protected]([A-Za-z0-9-]+\.)+([A-Za-z0-9]{2,4}|museum)$/i'), 
      'message' => 'Please provide a valid email address.', 
     ), 
     'isUnique' => array(
      'rule' => 'isUnique', 
      'message' => 'Email address already in use.', 
     ), 
     ) 

    ); 

    function equaltofield($check,$otherfield) 
    { 
     //get name of field 
     $fname = ''; 

     foreach ($check as $key => $value){ 
      $fname = $key; 
      break; 
     } 



     return $this->data[$this->name][$otherfield] === $this->data[$this->name][$fname]; 
    } 
    /*public function beforeValidate(){ 

     return true; 
    }*/ 
    public function validate_passwords() { 
      return $this->data[$this->alias]['password1'] === $this->data[$this->alias]['confirm_password']; 
    }