0

我启用了 clientValidation,甚至当我点击一个 textField 并将其留空时,例如 - Yii 验证 textField 和 dropDownList。

小部件:

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

下拉列表

$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'),
    );
}
4

1 回答 1

0

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

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

在 validate 之前的控制器操作中:

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

另请参阅文档: http ://www.yiiframework.com/doc/api/1.1/CActiveForm

于 2013-11-13T14:22:29.963 回答