2014-07-15 74 views
0

夥計們,CakePHP的數據驗證:如果至少一個字段被填充和多個規則沒有驗證

我想,以確保兩個領域(姓氏或電子郵件)被填充的至少一個檢查。每個領域也有多個規則。我正在使用CakePHP版本2.4.2。

現在的問題是,在更新和/或圍繞使用'last','allowEmpty','required'等進行多重排列之後,這些字段根本沒有驗證,或者在填充字段時不執行所有規則。

有關如何修改下面的代碼以實現以下行爲的任何建議,我們將非常感謝: 1.必須填充兩個字段中的一個; 2.連接到每個字段必須驗證以及其它的規則(即,如果最後的名稱被傳遞,則它必須是字符2和45之間和字母數字只)

下面是該模型的代碼:

public $validate = array(
    'last_name'=> array(
     'needOne' => array (
      'required' => FALSE, 
      'allowEmpty' => TRUE, 
      'last' => TRUE, 
      'rule' => array('checkOne','last_name'), 
      'message' => 'You must enter at least a contact name or email address.' 
     ), 
     'alphaNumeric' => array(
      'rule' => 'alphaNumeric', 
      'message' => 'Alphabets and numbers only' 
     ), 
     'between' => array(
      'rule' => array('between', 2, 45), 
      'message' => 'Between 2 to 45 characters' 
     ) 
    ), 
    'email' => array(
     'needOne' => array (
      'required' => FALSE, 
      'allowEmpty' => TRUE, 
      'last' => TRUE, 
      'rule' => array('checkOne','email'), 
      'message' => 'You must enter at least a contact name or email address.' 
     ), 
     'emailAddress' => array (
      'last' => TRUE, 
      'rule' => array('email', FALSE), 
      'message' => 'A valid Email address is required' 
     ) 
    ) 
); 

// Ensure at least the last name or email field value is provided 
function checkOne($field) { 
    if(!empty($this->data[$this->User]['last_name']) || 
     !empty($this->data[$this->User]['email'])){ 
     return true; 
    } else { 
     return false; 
    } 
} 

提前致謝!

回答

0

非常感謝vicocamacho。我堅持這樣做,除了你的建議之外,還發現解決方案還包括在視圖中添加'required'=> false。

下面是這個問題的任何其他人工作的解決方案:

型號:

public $validate = array(
    'last_name'=> array(
     'needOne' => array (
     'rule' => 'checkOne', 
     'message' => 'You must enter at least a contact name or email address.' 
    ), 
    'alphaNumeric' => array(
     'rule' => 'alphaNumeric', 
     'message' => 'Alphabets and numbers only', 
     'allowEmpty' => TRUE 
    ), 
    'between' => array(
     'rule' => array('between', 2, 45), 
     'message' => 'Between 2 to 45 characters', 
     'allowEmpty' => TRUE 
    ) 
), 
'email' => array(
    'needOne' => array (
     'rule' => 'checkOne', 
     'message' => 'You must enter at least a contact name or email address.' 
    ), 
    'emailAddress' => array (
     'rule' => 'email', 
     'message' => 'A valid Email address is required', 
     'allowEmpty' => TRUE 
    ) 
) 
); 

// Ensure at least the last name or email field value is provided 
public function checkOne($data) { 
    if(!empty($this->data[$this->alias]['last_name']) 
    || !empty($this->data[$this->alias]['email'])) { 
     return TRUE; 
    } else { 
     return FALSE; 
    } 
} 

視圖/場(我使用引導):

echo $this->Form->input('last_name', array(
     'required' => false, 
     'fieldset' => false, 
     'label' => false, 
     'before' => '<label class="control-label">Last Name <span class="one-required">*</span></label>', 
     'class' => 'form-control', 
     'placeholder' => 'Last Name', 
     'div' => 'form-group col-sm-12', 
     'error' => array(
      'attributes' => array(
       'wrap' => 'div', 
       'class' => 'alert alert-danger' 
      ) 
     ) 
    ) 
); 

echo $this->Form->input('email', array(
     'required' => false, 
     'fieldset' => false, 
     'label' => false, 
     'before' => '<label class="control-label">Email <span class="one-required">*</span></label>', 
     'after' => '', 
     'class' => 'form-control', 
     'div' => 'form-group col-sm-12 col-xs-12', 
     'error' => array(
      'attributes' => array(
       'wrap' => 'div', 
       'class' => 'alert alert-danger' 
      ) 
     ) 
    ) 
); 

謝謝。

0

編寫一個自定義規則,檢查兩個字段中的一個是否爲空,並在兩個字段上使用該規則。並且爲其他規則設置allowEmpty爲true。

0

您應該allowEmpty的一切,你也需要改變$this->User指數在checkOne$this->alias不檢查,如果字段存在,我更新了你的代碼,所以你可以明白我的意思的規則,否則,它會永遠返回false

public $validate = array(
    'last_name'=> array(
     'alphaNumeric' => array(
      'rule' => 'alphaNumeric', 
      'message' => 'Alphabets and numbers only', 
      'allowEmpty' => true 
     ), 
     'between' => array(
      'rule' => array('between', 2, 45), 
      'message' => 'Between 2 to 45 characters', 
      'allowEmpty' => true 
     ) 
    ), 
    'email' => array(
     'needOne' => array (
      'rule' => 'checkOne', 
      'message' => 'You must enter at least a contact name or email address.' 
     ), 
     'emailAddress' => array (
      'allowEmpty' => true, 
      'rule' => 'email', 
      'message' => 'A valid Email address is required' 
     ) 
    ) 
); 

// Ensure at least the last name or email field value is provided 
function checkOne($field) { 
     if(
      !empty($this->data[$this->alias]['last_name']) || 
      !empty($this->data[$this->alias]['email']) 
     ) 
     { 
       return true; 
     } else { 
       return false; 
     } 
} 
+0

非常感謝vicocamacho。我堅持這樣做,除了你的建議之外,還發現解決方案還包括在視圖中添加'required'=> false。我在下面列出了這個解決方案(太長了,不適合作爲評論:))。 – seba

相關問題