问题描述
我想知道如果任何Yii2专家能帮助我了解如何最佳使用Ajax的形式结合的Yii AJAX验证工作。我想我不会带你通过我的所有code能说明问题。
I'm wondering if any Yii2 experts can help me understand how best to work with ajax forms combined with Yii ajax validation. I think I can explain the issue without taking you through all of my code.
我的工作,用户输入自己的促销code到形式的促销code报名表上的形式是通过AJAX提交。然后,我们进行数据库查询的宣传片code的详细信息,验证code,如果code验证,我们要显示隐藏页面上的登记表。
I am working on a Promo Code entry form where the user enters their promo code into the form, the form is submit via ajax. We then perform a database lookup for the promo code details, validate the code and if the code validates, we want to display the registration form that is hidden on the page.
我有一个自定义的验证功能的表单字段code,这是一个名为注册的典范场景中活跃的领域。
I have a custom validation function for the form field "code", which is the active field in a model scenario named "register".
class UserCode extends ActiveRecord
{
...
public function scenarios()
{
return [
'register' => ['code'],
];
}
public function rules()
{
return [
[['code'], 'required'],
[['code'], 'validateUserCode', 'on' => ['register']],
];
}
public function validateUserCode($attribute, $params)
{
// perform all my custom logic to determine if the code is valid
if ($code_invalid) {
$this->addError($attribute, 'Sorry, this code is invalid.');
}
}
...
}
然后在控制器中,作为Yii2指南建议,我这个陷阱AJAX验证具有以下code:
Then in the controller, as the Yii2 Guide suggests, I trap this ajax validation with the following code:
public function actionValidate() {
$model = new UserCode(['scenario' => 'register']);
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
// no logic can be run after the above code b/c the form is submit with ajax
// and therefore always trapped in the Yii::$app->request->isAjax conditional
}
以上code一切工作正常,如果我从 $形式 - &GT移除焦点;场($模式,'code')
字段在我的形式,在Yii中的AJAX验证踢,并根据关我的自定义验证逻辑显示我的自定义错误消息。
The above code all works fine and if I remove focus from the $form->field($model, 'code')
field on my form, Yii's ajax validation kicks in and displays my custom error message based off of my custom validation logic.
当我去提交表单我的挑战出现。该表单提交通过AJAX也处理的,因此控制器动作总是返回的ActiveForm ::验证($模式)的结果;
,因为如果(Yii的:: $ APP->请求 - > isAjax和放大器;&安培; $建模>负载(的Yii :: $ APP->请求 - >后期()))
将获得适用于阿贾克斯表单验证和窗体上提交。
My challenge arises when I go to submit the form. The form submission is also handled through ajax, and therefore the controller action always returns the result of the ActiveForm::validate($model);
because if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()))
will get apply to both the ajax form validation AND on the form submit.
通过上述办法,我只好返回AJAX验证的唯一的结果,而不是说我可能需要额外的客户端验证,如后有效使用code显示登记表的任何JSON数据通过AJAX的形式提交。
With the above approach, I am forced to return only the results of the ajax validation and not any json data that I may need for additional client side validation, such as displaying the registration form after a valid use code is submitted through the ajax form.
我知道我可以设置'enableAjaxValidation'=>假
上的ActiveForm,然后返回如果(的Yii :: $ APP-&GT在我自己的JSON数据;请求 - > isAjax和放大器;&安培; $建模>负载(Yii的:: $ APP->请求 - >后期()))
状态。如果我这样做,我能够证明登记表,因为我有自己的JSON数据的工作。
I realize that I can set 'enableAjaxValidation' => false
on the ActiveForm and then return my own json data inside the if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()))
condition. If I do this, I am able to show the registration form because I have my own json data to work with.
有没有办法让AJAX验证一个被提交与阿贾克斯的形式吗?你怎么能捕获单独的AJAX验证从AJAX表单提交以不同的方式处理这两个事件?
Is there a way to have ajax validation on a form that is submitted with ajax? How could you trap the ajax validation separately from the ajax form submission to handle the two events in different manners?
任何建议或其他方法都大大AP preciated!
Any suggestions or alternate approaches are GREATLY appreciated!
推荐答案
您应该建立 validationUrl
用不同的URL相比,您所提交表单的网址。通过这种方式,你可以有验证功能将验证并返回返回的ActiveForm ::验证($模式);
和正常提交表单,做别的事情
You should set up validationUrl
with a different URL compared to the URL that you are submitting the form to. In this way you can have the validation function that would validate and return the return ActiveForm::validate($model);
and the normal submit form that does something else.
您可以阅读更多关于 validationUrl
<一个href="http://www.yiiframework.com/doc-2.0/yii-widgets-activeform.html#$validationUrl-detail">here:
You can read more about validationUrl
here:
这篇关于Yii2:一个提交的Ajax表单Ajax的表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!