2013-12-22 151 views
0

這裏我已經驗證,如果任何人輸入超過6位數字後提交,他將得到消息'不超過6位!!'但在這裏我想做一個另一個驗證,發送數據的用戶將可以在字段中輸入超過六個字符。我可以如何解決它?cakephp字段輸入限制

這裏我的模型代碼

public $validate = array(
     'name' => array(
       'rule' => array('notempty'), 
       'message' => 'Name is required and cannot be empty!!!', 
       'required' => true, 
     ), 
     'ssc_roll' => array(
       'rule' => array('notempty'), 
       'message' => 'S.S.C Roll is required and cannot be empty!!!', 
       'rule' => array('maxLength', 6), 
       'message' => 'Not more than 6 digits!!', 
       'required' => true, 
     ), 
     'hsc_roll' => array(
       'rule' => array('notempty'), 
       'message' => 'H.S.C Roll is required and cannot be empty!!!', 
       'rule' => array('maxLength', 6), 
       'message' => 'Not more than 6 digits!!', 
       'required' => true, 
     ), 
     'date_of_birth' => array(
       'rule' => array('date'), 
       'message' => 'Enter a valid date.', 
       'required' => true, 
     ), 
     'mobile' => array(
       'rule' => array('notempty'), 
       'message' => 'Enter a valid mobile number.', 
       'required' => true, 
     ) 

); 

,並在此視圖代碼

<div class="control-group"> 
       <label class="control-label" for="name">S.S.C Roll *</label> 
<div class="controls"> 
<?php 
echo $this->Form->input('ssc_roll', array('class'=>'input-large')); 
echo $this->Form->error('ssc_roll'); ?> 
</div> 
</div> 
<div class="control-group"> 
<label class="control-label" for="name">H.S.C Roll *</label> 
<div class="controls"> 
<?php echo $this->Form->input('hsc_roll', array('class'=>'input-large')); 
echo $this->Form->error('hsc_roll'); ?> 
</div> 
</div> 
+0

哪裏是在給定的代碼中的6個位數驗證?無論如何,如果您確定了「數字<7」的驗證並且您希望用戶能夠編寫更多內容,只需刪除驗證? – Sugar

+0

我剛剛編輯了我的code.Here驗證code.Here我希望用戶將被啓用輸入字段中輸入超過6位數字。 –

回答

0

如果要驗證在另一場允許數字新號碼,修改maxlength規則相匹配,以最大允許的位數:

// 4 digits instead of 6 
    'zip_code' => array(
      'rule' => array('notempty'), 
      'message' => 'Zip code is required and cannot be empty!!!', 
      'rule' => array('maxLength', 4), 
      'message' => 'Not more than 4 digits!!', 
      'required' => true, 
    ), 
    // 10 digits instead of 6 
    'phone_number' => array(
      'rule' => array('notempty'), 
      'message' => 'Phone number is required and cannot be empty!!!', 
      'rule' => array('maxLength', 10), 
      'message' => 'Not more than 10 digits!!', 
      'required' => true, 
    ), 
+0

這裏沒有任何驗證問題,因爲我想驗證。我想要在輸入字段中進行另一種驗證,例如郵政編碼。如果您要在字段中鍵入郵政編碼,則不能輸入超過4位數字。 –

+0

編輯我的答案,驗證來自maxLength屬性,只需將其修改爲任何你想要的。無論如何,只要檢查CakePhP參考關於這些控件,知道你可以調整哪些屬性來獲得你想要的驗證,比每次需要一個新的驗證都要快。 – Sugar

0

您可以根據您的b usiness邏輯

if ($userCanInputMoreThanSixCharacter === true) { 
    $this->validator()->getField('ssc_roll')->setRule('maxlength', array(
     'rule' => array('maxlength',10), 
     'required' => true 
    )); 
} 

,或者如果條件爲真,您可以刪除驗證

if ($userCanInputMoreThanSixCharacter === true) { 
    $this->validator()->remove('ssc_roll'); 
}