renderPartial clientSide验证不起作用。我想用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 );
}
);'
]);
Controller.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);
}
答案 0 :(得分:3)
如果要启用客户端验证,请将此属性设置为true。
$form = ActiveForm::begin([
'options' => [
'enableAjaxValidation' => true,
'enableClientValidation'=>true
]
]);
使用renderAjax()函数代替renderPartial(),它将使用JS / CSS脚本和在视图中注册的文件注入到渲染结果中
答案 1 :(得分:0)
在Controller.php
中,您需要将layout
设置为false
并die
执行
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;
}
答案 2 :(得分:0)
您没有将任何验证错误从控制器返回到您的视图。 存档使用
YII \部件\的ActiveForm ::验证($ 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]);
}
}