yii2-usuario icon indicating copy to clipboard operation
yii2-usuario copied to clipboard

Ajax Captcha not working

Open srakl opened this issue 3 years ago • 0 comments

How do i get yii2-usuario recaptcha to work with Ajax posts?

Recaptcha

in my model i have this

public $captcha;

public function rules() {
		return [
                             [['captcha'], 'required'],
                             [['captcha'], 'Da\User\Validator\ReCaptchaValidator'],
                             ....
                             ];
}

in my view

use Da\User\Widget\ReCaptchaWidget;

<form>
<?= $form->field($model, 'captcha')->widget(ReCaptchaWidget::class, ['theme' => 'light'])->label(false) ?>
</form>

<script>
            $(document).ready(function () {  
                var $form = $("#<?= $model->formName() ?>");
                $form.on("submit", function (event, messages) {
                    event.preventDefault();
                    $.ajax({
                        "type":"POST",
                        "url":$form.attr('action'),
                        "data":$form.serialize(),
                        "dataType":"json",
                        "success":function(data){
                            $("#tax").html(data.tax);	
                        },                                        
                    });
                    return false;		
                });
            });
</script>

in my controller

                $model = new calcForm;
		$model->scenario = calcForm::SCENARIO_CALC;
		if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
			if ($model->validate())
			{
                              ....
                         }
                 }

How do i get the validation to work with an ajax post? $model->validate() in my controller doesn't seem to validate the captcha. Thanks

srakl avatar Nov 13 '22 06:11 srakl