Ajax validation and Captcha


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.

  1. 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?

  1. 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);



    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


Just a shot in the dark here try removing your enableajax validation and using clientvalidtaion. Make sure you put an Id too


$form = ActiveForm::begin([

   	'options' => [

        	'id' => 'signup-form',

        	'enableClentValidtaion' => true,




If it works you shouldn’t need anything extra in your controller other than the default stuff i.e. no response, is ajax, scenarios, etc. but like i said it’s just a shot in the dark. It’s what I use on regular ajax / pjax forms.


I would like to check on the fly for the uniqueness of username and email, when losing focus on these fields.

With enableclientvalidation (enabled by default) this doesn’t seem possibile (the check of uniqueness occur only after submitting the form), so I ended up with enableajaxvalidation .

The problem is that even if I set enableajaxvalidation only for that two fields, captcha validation doesn’t work, until I exclude it setting a scenario.