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

enableClientValidation not work on submitting if set enableAjaxValidation to true #14917

Open
phuczeero opened this issue Oct 5, 2017 · 17 comments
Labels
JS JavaScript related status:ready for adoption Feel free to implement this issue. type:bug Bug

Comments

@phuczeero
Copy link

phuczeero commented Oct 5, 2017

What steps will reproduce the problem?

My model:

class SignupForm extends \yii\base\Model
{
    public $email;
    public $password;

    public function rules()
    {
        return [
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'unique', 'targetClass' => User::className()],

            ['password', 'required'],
        ];
    }

    public function signup()
    {
        if (!$this->validate()) {
            return null;
        }

        $user = new User;
        $user->email = $this->email;
        $user->setPassword($this->password);
        return $user->save();
    }
}

My controller action:

public function actionSignup()
{
    $model = new SignupForm();
    if ($model->load(Yii::$app->request->post())) {
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }

        if ($model->signup()) {
            return $this->goHome();
        }
    }

    return $this->render('signup', [
        'model' => $model,
    ]);
}

My form:

<?php
    $form = ActiveForm::begin([
        'id' => 'form-signup',
        'action' => ['site/signup'],
        'enableAjaxValidation' => true,
]); ?>

    <?= $form->field($model, 'email', ['inputOptions' => ['autofocus' => 'autofocus']])->input('email') ?>

    <?= $form->field($model, 'password')->passwordInput() ?>

    <?= Html::submitButton('Signup') ?>

<?php ActiveForm::end(); ?>

What is the expected result?

Validate all supported client validations before sending ajax validation request

What do you get instead?

Client validation work correctly on input blur, change event, but send ajax request on submitting although all fields are empty.

Additional info

Q A
Yii version 2.0.12
PHP version 7.1.10
Operating system Debian
@phuczeero
Copy link
Author

Why 2.0.12 force ajax validation on submitting

yii.activeForm.js

if (needAjaxValidation && ($.isEmptyObject(messages) || data.submitting)) {
}

#11228

@samdark samdark added the JS JavaScript related label Oct 5, 2017
@samdark
Copy link
Member

samdark commented Oct 5, 2017

Is it OK in 2.0.11?

@phuczeero
Copy link
Author

@samdark 2.0.11 not OK.

@samdark
Copy link
Member

samdark commented Oct 6, 2017

Would be good if you'll find out if there's a version of Yii that is OK.

@samdark samdark added this to the 2.0.16 milestone Oct 6, 2017
@schmunk42
Copy link
Contributor

Client validation work correctly on input blur, change event, but send ajax request on submitting although all fields are empty.

@phuczeero Does this behavior only occur if all fields are empty or also if there are wrong values in them?

@phuczeero
Copy link
Author

@schmunk42 As I mentioned above, enableClientValidation and enableAjaxValidation are set to true, all fields are required, and all supported client validation still work correctly until I hit the submit button, the form skip client validation and send ajax validation request to server. I think it OK in this commit e5bcbf4

@schmunk42
Copy link
Contributor

I see, I was just asking if there is a difference between all inputs empty and one or more wrong values.

Could you verify this for debugging without those two rules?

     ['email', 'email'],
     ['email', 'unique', 'targetClass' => User::className()],

but just

     ['email', 'required'],

I remember some strange behavior when using validators, which required server-side validation, but I thought it would be project specific.

@phuczeero
Copy link
Author

I removed unique validation and the form still skip client validation and force ajax validation.

@phuczeero
Copy link
Author

Change yii.activeForm.js file and it works.

From

if (needAjaxValidation && ($.isEmptyObject(messages) || data.submitting)) {
    // ...
}

to

if (needAjaxValidation && $.isEmptyObject(messages)) {
    // ...
}

@samdark samdark modified the milestones: 2.0.16, 2.0.14 Oct 11, 2017
@samdark samdark added the type:bug Bug label Feb 6, 2018
@samdark samdark modified the milestones: 2.0.14, 2.0.15 Feb 6, 2018
@hooman-mirghasemi
Copy link
Contributor

I have same Problem .
I do changes @phuczeero write on top comment.
Thank you @phuczeero .

@samdark is it ok? and have not bad effect I upload my project on real host with this change? (until new release 2.0.16 )

@samdark
Copy link
Member

samdark commented Jun 12, 2018

No idea. I haven't tested it. If you'll find it's good, let us know.

@hooman-mirghasemi
Copy link
Contributor

hooman-mirghasemi commented Jun 12, 2018

I use it and change yii.activeForm.js in vendor. it seems work correctly!
but i only use it in my localhost , so That's beter to yii team member test it and find best way.

@samdark samdark added the status:ready for adoption Feel free to implement this issue. label Jun 12, 2018
@s1lver
Copy link
Member

s1lver commented Aug 6, 2018

Reproduced, but, I think that is not exactly true.

@machour
Copy link
Member

machour commented Jan 14, 2019

@s1lver could you elaborate?

@machour machour modified the milestones: 2.0.16, 2.0.17 Jan 14, 2019
@s1lver
Copy link
Member

s1lver commented Jan 28, 2019

@machour perhaps I have doubts either in the way of correcting or in reproducing the error. Now I can not remember exactly

@samdark samdark removed this from the 2.0.17 milestone Jan 28, 2019
@hooman-mirghasemi
Copy link
Contributor

Why don't resolve this bug it in 2.0.16.1 milestone?

@machour
Copy link
Member

machour commented Feb 8, 2019

@hooman-pro We don't use milestones for such versions.
If you have a fix, please open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JS JavaScript related status:ready for adoption Feel free to implement this issue. type:bug Bug
Projects
None yet
Development

No branches or pull requests

8 participants