2013-11-20 76 views
1

下面的模型擴展了CFormModel,我需要提及規則,以使屬性只允許整數。Yii - 如何驗證文本字段僅用於整數

class SearchForm extends CFormModel { 

    public $min_size; 
    public $max_size; 

    /** 
    * Declares the validation rules. 
    * The rules state that username and password are required, 
    * and password needs to be authenticated. 
    */ 
    public function rules() { 
array('min_size, max_size', 'numerical', 'integerOnly'=>true), 

     ); 
    } 

文本字段創建使用 -

<?php echo $form->textField($model,'min_size', array('placeholder' => 'Min Sqft', 'style'=>'width:100px')); ?> 

但是,上面的驗證是行不通的。我如何驗證文本字段只允許整數。反正我有可以做驗證

+2

郵控制器代碼。你打電話驗證()? – redGREENblue

+0

嘗試將類型驗證程序也添加到這些字段。 http://www.yiiframework.com/doc/api/1.1/CTypeValidator –

回答

3

試試這個

public function rules(){ 
return array(
    array('min_size, max_size', 'numerical', 'integerOnly'=>true));} 
+0

查看_form.php for $ form = $ this-> beginWidget('CActiveForm',array( \t'id'=>'member-form ', \t'enableAjaxValidation'=> true,//設置爲true )); –

+1

<?php echo $ form-> errorSummary($ model); //在form.php中取消註釋此代碼?> –

+0

也在控制器中籤出 –

2

使用像V2

[['mobile'], 'integer'], 
[['mobile'], 'string', 'min' => 10, 'max' => 10], 
+0

試着詳細說明爲什麼這會起作用。由於它不會幫助任何人理解爲什麼/你的答案如何解決OP問題。 – Tensibai