2amigos / yii2-usuario

Highly customizable and extensible user management, authentication, and authorization Yii2 extension
https://github.com/2amigos/yii2-usuario
Other
294 stars 142 forks source link

Ajax Captcha not working #481

Open srakl opened 1 year ago

srakl commented 1 year ago

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