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

Suggestion: determine whether validation was successful in js activeform "afterValidate" event #167

Open
vercotux opened this issue Mar 28, 2017 · 5 comments
Assignees
Labels

Comments

@vercotux
Copy link

Seems like a very trivial, common requirement, but there doesn't seem to be any simple way to know whether validation was successful inside the afterValidate client event. How do we do it?

This would fit perfectly into the Forms activeform js chapter.

@samdark samdark self-assigned this Mar 28, 2017
@samdark
Copy link
Owner

samdark commented Mar 29, 2017

Can't you check length of errorAttributes?

function (event, messages, errorAttributes) {
    if (errorAttributes.length) {
        // not valid
    } else {
       // valid
    }
}

@vercotux
Copy link
Author

vercotux commented Mar 29, 2017

That was what I initially tried, but it does not work, because errorAttributes does not contain all validation errors, so you will get false positives. It only contains validation errors of fields rendered by ActiveForm! Meaning if you used something like

<?= Html::activeHiddenInput($model, 'test'); ?>

then test would not be part of errorAttributes even when it has validation errors!

Now, this would all sort of make sense, if we were dealing purely with client-side validation. After all, client-side does not know if there are any validation errors on the server-side... until you introduce AJAX validation! :)

With AJAX validation, we do know if there were server-side validation errors, so we should be able to know if validation was successful in the afterValidate event. But how? We cannot rely on errorAttributes because it does not contain server-side validation errors even when using AJAX validation. Maybe this is a bug?

@samdark
Copy link
Owner

samdark commented Mar 29, 2017

According to code it should be available in AJAX validation as well. Please create a ticket in the main Yii 2.0 repo with a clear way to reproduce the issue. Thanks.

@vercotux
Copy link
Author

vercotux commented Mar 30, 2017

Alright. In the meantime I have found a working solution. Turns out that messages inside afterValidate will always contain error messages (including those coming from AJAX validation!), no matter how we rendered our ActiveForm. We can use this function to reliably determine whether validation was successful:

function isValid(messages) {
    for (var i in messages) {
        if (messages[i].length>0) {
            return false;
        }
    }
    return true;
}

Usage example:

function (event, messages, errorAttributes) {
    if (isValid(messages)) {
        // valid
    } else {
       // not valid
    }
}

@samdark
Copy link
Owner

samdark commented Mar 30, 2017

Great. Would you like to make a pull request adding it to recipe?

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

No branches or pull requests

2 participants