Only catches first error
ronaldroe opened this issue · 3 comments
For some reason, only the first error caught is getting pushed to the errors argument (or the object.errors for that matter). Code below:
var facility_val;
$(document).ready(function(){
// Facility client-side validation
facility_val = new FormValidator('facility_form', [
{
name: 'name',
display: 'Name',
rules: 'required'
},
{
name: 'sales_tax_percent',
display: 'Sales Tax Percent',
rules: 'required|less_than[100]'
},
{
name: 'healthways_id',
display: 'Healthways ID',
rules: 'exact_length[16]'
},
{
name: 'facility_ip',
display: 'Facility IP Address',
rules: 'required'
},
{
name: 'address1',
display: 'Address Line 1',
rules: 'required'
},
{
name: 'city',
display: 'City',
rules: 'required'
},
{
name: 'state_abbr',
display: 'State Abbreviation',
rules: 'required'
},
{
name: 'zip',
display: 'Zip',
rules: 'required|integer'
}
], function(errors, evt){
$('span.error').html('').hide();
if(errors.length > 0){
for(var i = 0; i < errors.length; i++){
$(errors[i].element).parent('p').children('span.error').html(errors[i].message).css('display', 'inline-block');
}
}
console.log(errors);
});
$('#facility_form input').on('change', function(){
facility_val._validateForm();
});
The error is correctly displayed and removed as required, but only the first error, as determined by the rules array, is displayed or even pushed to the errors array.
This is by design. We don't want to overload the user with a ton of errors if they submit with a blank field for example.
That would explain why it won't return multiple errors. Thank you
I'm only ever getting one error, no matter how many fields should be throwing them. I think, but am not sure, that what you're saying is by design is that if a single field fails multiple validators, it only throws the first failure?
If I correct the first field, then the second one shows up ... but not until then. So my "errors" array is only ever one object, no matter how many fields I leave blank.
I'm fine with this being the intended design, but the demo page behaves differently (http://rickharrison.github.io/validate.js/), which threw me off and sent me looking for whether I was doing something wrong when only one error showed up after I submitted a form that should've produced four errors (one each for four separate fields). The page also suggests the following code as a callback:
function(errors, event) {
if (errors.length > 0) {
var errorString = '';
for (var i = 0, errorLength = errors.length; i < errorLength; i++) {
errorString += errors[i].message + '<br />';
}
el.innerHTML = errorString;
}
}
Which also makes it seem like we should be expecting multiple errors to be passed, but that's not the behavior I'm getting.
So my question is: am I experiencing a bug, or is it working as designed?
Thanks!