Closed dhoomm closed 9 years ago
My temporary solution:
Add validation option skipOnError=true
inside runValidations
function
inside for (attr in constraints) {
//Skip validation if error already exist Line 77
if (validatorOptions.skipOnError === true) {
var result = 0;
var hasError = false;
for (result in results) {
if (results[result].attribute === attr) {
if (results[result].error) {
hasError = true;
break;
}
}
}
if (hasError === true) {
continue;
}
}
//Skip validation if error already exist
I am sure you can come up with a better solution.
I understand your concern, "fail fast" is a valid use case. However, if you, as a user, have 5 errors in a form it's quite frustrating having to submit once per error so we could probably want to run all validations and then just ignore the result from the async ones if the flag is on.
I'll give some thought to this but I think that for now you can easily enough extend validate.js yourself.
My current solution works that way. All validations not having flag 'skipOnError' are performed.
At the moment how i have extended it is: In validation rules we have to mention e.g.
username: {
presence: true,
format: {
pattern: /^[a-zA-Z]*[0-9]*$/, // Only alphabets: /^[a-zA-Z]*$/,
message: "can only contain alphabets and can end with numbers"
},
serverSideValidate: {url: 'site/validate-register', skipOnError: true},
length: {minimum: 6, maximum: 16}
},
Here skipOnError
prevents validation only for 'serverSideValidate' validator. The 'presence', 'length' and 'pattern' validation are still performed.
So in above example if I enter e.g. 'a#d', I get errors for both length and pattern.
One problem is that 'skipOnError' as the first validator of that field is useless, as no previous validations are performed and it will always be executed.
I would actual say that people should use two validation schemas for this use case, one for async and one for regular.
Would you mind moving this do https://github.com/ansman/validate.js All further development will happen there.
Async validations are mostly server side validations, so if a field is invalid on client site, it will always be invalid on server side. e.g. If 'username' is invalid, it is no use to check if it is available