Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajax Captcha not working #481

Open
srakl opened this issue Nov 13, 2022 · 0 comments
Open

Ajax Captcha not working #481

srakl opened this issue Nov 13, 2022 · 0 comments

Comments

@srakl
Copy link

srakl commented Nov 13, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant