Hello,
I’ve extended the signup form of the advanced template with ajax validation and a captcha.
I’m aware of the issue about performing an AJAX validation on a Captcha field ( http://www.yiiframework.com/doc-2.0/yii-captcha-captchavalidator.html ) - it seems that passing AJAX validation on a captcha field triggers the generation of a new image, so the POST validation fails when submitting the form.
I’d like to know the most correct strategies/workaround to make this form work properly.
- I tried to set enableAjaxValidation only for the ActiveField (username, email) involved in ajax check, rather than enabling it for the whole ActiveForm, but it doesn’t work… it seems that even if the captcha has not enableAjaxValidation set, some sort of validation is triggered and when the form is finally submitted the captcha is checked against a new image/captcha.
Is this correct?
- The only solution I’ve found is to set a scenario 'captchavalidation" that avoids validation of captcha when AJAX validation is performed, but I think it’s a not-so-clean workaround…
In SiteController.php:
…
public function actionSignup()
{
$model = new SignupForm();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
$model->scenario='captcharequired';
if ($model->load(Yii::$app->request->post())) {
…
In SignupForm.php:
public function rules()
{
return [
.......
['verifycode', 'captcha','message' => 'Wrong verify code.','on'=>'captcharequired']
];
}
Is there a cleaner way to perform the same task without involving scenarios?
Thank you
EG