使用 pjax 进行 activeform 客户端验证
activeform client validation with pjax
当 enableClientValidation 设置为 true 时,yii2-pjax 小部件不会触发 ajax。只有当 enableClientValidation 设置为 false 时,pjax 才起作用。有什么方法可以在每个字段上(通过 yii)以及提交按钮上的 pjax(通过 pjax)ajax 进行活动表单客户端和 ajax 验证
<?php Pjax::begin(['id'=> 'new-comment','enablePushState' => false]); ?>
<?php $form = ActiveForm::begin([
'id' => $model->formName(),
'options' => ['data-pjax' => "1"] ,
'action' => ['site/signup'],
'enableClientValidation' => true,
]);
?>
<?= Html::submitButton('REGISTER', ['class' => 'btn btn-primary', 'name' => 'signup-button', 'id'=>'register-btn']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?
我不得不在 PHP 中删除 Pjax 调用,因为否则 clientValidation 会失败。
<?php $form = ActiveForm::begin([
"id"=>"CreateForm",
'enableClientValidation' => true,
//'enableAjaxValidation' => true,
'validationUrl'=>['site/form-validate'],
"options" => ["enctype" =>"multipart/form-data"]
]
); ?>
enableAjaxValidation 也可以在上面设置为真
目前,我的 clientValidation 为 true,没有基于 validationUrl 进行验证的 Pjax。表单的提交按钮在 javascript 中进行外部处理,它将首先调用相同的 validationUrl 并使用 yii activeform 的 updateMessages 方法更新错误字段。如果没有错误,则业务逻辑在与 validationUrl 操作不同的操作中完成
$.ajax({
url : encodeURI(baseUri + "site/form-validate"),
data : $("#CreateForm").serialize(),
dataType : 'json',
type : 'POST',
beforeSend : function(xhr, settings) {
$this.attr("disabled","disabled");
Pace.stop();
Pace.bar.render();
},
success : function(data) {
$('#CreateForm').yiiActiveForm("updateMessages", data);
if($("form#CreateForm").find('.has-error').length == 0) {
$.ajax({
url : encodeURI(baseUri + "site/form"),
data : $("#CreateForm").serialize(),
dataType : 'json',
type : 'POST',
beforeSend : function(xhr, settings) {
},
success : function(data) {
console.log(data);
},
error : function(data) {
},
complete : function() {
$this.removeAttr("disabled");
Pace.stop();
},
});
} else {
$this.removeAttr("disabled");
Pace.stop();
}
},
error : function(data) {
},
complete : function() {
},
});
控制器-
public function actionFormValidate() {
$model = new CreateForm();
if(Yii::$app->request->isAjax && $model->load($_POST))
{
Yii::$app->response->format = 'json';
return \yii\widgets\ActiveForm::validate($model);
}
}
当 enableClientValidation 设置为 true 时,yii2-pjax 小部件不会触发 ajax。只有当 enableClientValidation 设置为 false 时,pjax 才起作用。有什么方法可以在每个字段上(通过 yii)以及提交按钮上的 pjax(通过 pjax)ajax 进行活动表单客户端和 ajax 验证
<?php Pjax::begin(['id'=> 'new-comment','enablePushState' => false]); ?>
<?php $form = ActiveForm::begin([
'id' => $model->formName(),
'options' => ['data-pjax' => "1"] ,
'action' => ['site/signup'],
'enableClientValidation' => true,
]);
?>
<?= Html::submitButton('REGISTER', ['class' => 'btn btn-primary', 'name' => 'signup-button', 'id'=>'register-btn']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?
我不得不在 PHP 中删除 Pjax 调用,因为否则 clientValidation 会失败。
<?php $form = ActiveForm::begin([
"id"=>"CreateForm",
'enableClientValidation' => true,
//'enableAjaxValidation' => true,
'validationUrl'=>['site/form-validate'],
"options" => ["enctype" =>"multipart/form-data"]
]
); ?>
enableAjaxValidation 也可以在上面设置为真
目前,我的 clientValidation 为 true,没有基于 validationUrl 进行验证的 Pjax。表单的提交按钮在 javascript 中进行外部处理,它将首先调用相同的 validationUrl 并使用 yii activeform 的 updateMessages 方法更新错误字段。如果没有错误,则业务逻辑在与 validationUrl 操作不同的操作中完成
$.ajax({
url : encodeURI(baseUri + "site/form-validate"),
data : $("#CreateForm").serialize(),
dataType : 'json',
type : 'POST',
beforeSend : function(xhr, settings) {
$this.attr("disabled","disabled");
Pace.stop();
Pace.bar.render();
},
success : function(data) {
$('#CreateForm').yiiActiveForm("updateMessages", data);
if($("form#CreateForm").find('.has-error').length == 0) {
$.ajax({
url : encodeURI(baseUri + "site/form"),
data : $("#CreateForm").serialize(),
dataType : 'json',
type : 'POST',
beforeSend : function(xhr, settings) {
},
success : function(data) {
console.log(data);
},
error : function(data) {
},
complete : function() {
$this.removeAttr("disabled");
Pace.stop();
},
});
} else {
$this.removeAttr("disabled");
Pace.stop();
}
},
error : function(data) {
},
complete : function() {
},
});
控制器-
public function actionFormValidate() {
$model = new CreateForm();
if(Yii::$app->request->isAjax && $model->load($_POST))
{
Yii::$app->response->format = 'json';
return \yii\widgets\ActiveForm::validate($model);
}
}