Yii在聚焦之前验证CActiveForm dropDownList

时间:2013-11-12 22:09:31

标签: php yii

我启用了clientValidation,即使我点击textField并将其保留为空,例如 - Yii验证textField和dropDownList。

窗口小部件:

$form=$this->beginWidget('CActiveForm', array(
    'id'=>'service-form',
    'enableClientValidation'=>true,        
    'clientOptions'=>array(
        'validateOnSubmit'=>true,
),

dropDownList

$models = Countries::model()->findAll();

$list = CHtml::listData($models, 'countryname', 'countryname');                               

echo $form->dropDownList($model, 'country', $list, array('empty' => 'Select a country...'));
echo $form->error($model,'country');

规则()

public function rules()
{
    return array(
        array('name, country, postal_code, city, adress, description', 'required'),
        array('name', 'unique','className'=>'Item','attributeName'=>'name','message'=>"Company already exists"),
        array('postal_code', 'numerical', 'integerOnly'=>true),
        array('name, country, adress, description', 'length', 'max'=>255),
        array('city', 'length', 'max'=>150),
        array('id, name, country, postal_code, city, adress, description, category, create_date', 'safe', 'on'=>'search'),
    );
}

1 个答案:

答案 0 :(得分:0)

您需要在CActiveForm小部件中使用“enableAjaxValidation”。

$form=$this->beginWidget('CActiveForm', array(
    'id'=>'service-form',
    'enableAjaxValidation'=>true
    'enableClientValidation'=>true,        
    'clientOptions'=>array(
        'validateOnSubmit'=>true,
),

在验证前的控制器操作中:

if(Yii::app()->getRequest()->getIsAjaxRequest()) {
    echo CActiveForm::validate( array( $model )); 
    Yii::app()->end(); 
}

另见doc: http://www.yiiframework.com/doc/api/1.1/CActiveForm