0

renderPartial 客户端验证不起作用。我想用ajax渲染部分表单。例如: _form.php

$form = ActiveForm::begin([
    'options' => [
        'enableAjaxValidation' => true,
    ]
]); 
$form->field($model, 'category_id')->dropDownList($category, [
'onchange'=>'
    $.get( "'.Url::toRoute('/controller/params').'", { id: $(this).val() } )
           .done(function( data ) {
                     $( "#offers-param-content" ).html( data );
           }
     );'
]);

控制器.php

public function actionParams($id)
{
    $model = new Param();
    $params = EavAttribute::find()->where(['category_id'=>$id])->all();
    $this->renderPartial('_params', ['model' => $model, 'params' => $params];
}

_params.php

foreach($params as $item){
    echo Html::activeTextInput('text', $model, $item->name);
}
4

3 回答 3

4

如果要启用客户端验证,请将此属性设置为 true。

$form = ActiveForm::begin([
    'options' => [
        'enableAjaxValidation' => true,
    'enableClientValidation'=>true

    ]
]); 

并使用 renderAjax() 函数代替 renderPartial() 它将使用 JS/CSS 脚本和在视图中注册的文件注入渲染结果

于 2016-02-01T08:30:45.783 回答
0

您没有将任何验证错误从您的控制器返回到您的视图。归档使用

yii\widgets\ActiveForm::validate($yourModel);

如果您不使用 activeform,您可以通过以下方式返回错误

$model->getErrors();//will return errors from $model->validate()

从你的摘录中试试这个

public function actionParams($id) {
        $model = new Param();
        if ($model->load(Yii::$app->request->post())) {
            if (Yii::$app->request->isAjax) {
                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
                return ActiveForm::validate($model); /* Validation error messages are returned by this static function */
            }
            $params = EavAttribute::find()->where(['category_id' => $id])->all();
            $this->renderPartial('_params', ['model' => $model, 'params' => $params]);
        }
    }
于 2016-02-01T06:17:11.210 回答
0

Controller.php你需要layout设置falsedie执行

public function actionParams($id)
{
    $this->layout = false;
    $model = new Param();
    $params = EavAttribute::find()->where(['category_id'=>$id])->all();
    $this->renderPartial('_params', ['model' => $model, 'params' => $params];
    die;
}
于 2016-01-31T17:37:57.617 回答