bootstrap-validator icon indicating copy to clipboard operation
bootstrap-validator copied to clipboard

Custom validations blank value

Open gvillo opened this issue 9 years ago • 8 comments

I want to validate an input text with blank (empty) value using custom validators.

I changed this:

$.each(Validator.VALIDATORS, $.proxy(function (key, validator) {
  var value = getValue($el);
  if ((value == '' || value || $el.attr('required')) &&
      ($el.data(key) || key == 'native') &&
      !validator.call(this, $el)) {
    var error = getErrorMessage(key)
    !~errors.indexOf(error) && errors.push(error)
  }
}, this))

gvillo avatar May 19 '16 14:05 gvillo

This must be the cause of what I'm experiencing as well. I'm writing a custom validator for a radio group that starts unselected and is only required if another field has a value. The custom validator never runs because for the radio group value is blank. I will test this edit to see if it resolves my problem.

Jargon64 avatar Jun 23 '16 21:06 Jargon64

Yup, that has fixed my issue. +1

Jargon64 avatar Jun 23 '16 21:06 Jargon64

+1 for fixing this issue

ghost avatar Aug 24 '16 15:08 ghost

For me it's also a case when my custom validator don't work, because value is empty

desfero avatar Feb 14 '17 13:02 desfero

I can update PR to avoid conflicts with master, is there any reason to avoid merging this @1000hz ?

gvillo avatar Jul 31 '17 18:07 gvillo

This solution didn't worked for me. In my case, I wanted an input to be validated only if another input has been filled. For this, I created a custom validator called "requiredif". I applied this solution but, after that, then ALL required validators started to fire as invalid. Besides that, this solution didn't seems to fix my original problem, because my custom validator was not being called anyway.

The reason for that was (I believe) on line 44:

this.validators = $.extend({}, Validator.VALIDATORS, options.custom)

The code does use Validator.VALIDATORS object, that was not being expanded with the content of options.custom (which I validated by checking the content of VALIDATORS object on the runValidators method). To fix this, I changed that line to:

this.validators = $.extend(Validator.VALIDATORS, options.custom)

And that did the trick to get my custom validator called. The other part of my solution was to use a custom data property called data-force-validation to instruct runValidators method to run a validator even if the target is empty and is not required (which is exactly what I wanted in the first place). So, the code ended up like this:

$.each(Validator.VALIDATORS, $.proxy(function (key, validator) {
  var value = getValue($el);
  var forced = $el.data("force-validation");
  if ((value ||  $el.attr('required') || forced) &&
      ($el.data(key) || key == 'native') &&
      !validator.call(this, $el)) {
    var error = getErrorMessage(key)
    !~errors.indexOf(error) && errors.push(error)
  }
}, this))

and the custom validator:

customValidators.requiredif = function($el) {
   var $ref = $($el.data('requiredif')),
         err = $el.data("requiredif-error") || "This value is required";
   if ($ref.val() && !$el.val()) return err;
} // requiredif
<input name="password" id="password"/>
<input name="pwdconfirm" data-requiredif="#password" data-requiredif-error="Confirm your new password" data-force-validation/>

Hope this helps anyone facing this same problem.

vespina avatar Sep 17 '17 20:09 vespina

I've got a group of checkboxes, and I have to validate different combinations. (Depending on the case, it might be only one required, choose n, or all of them are required).

I believe, running validators on empty inputs (unchecked checkboxes, in this case) is an absolute must for this. Otherwise I won't ever notice, if the user unchecks them all.

Can we perhaps do something to mark a validator to even run on a blank input for a certain field? (Please also see issue #635)

fancsali avatar Oct 04 '19 11:10 fancsali

To be able to validate checkboxes and radio buttons, the default should definitely be to run the custom validators for optional fields as well; and then for easier use one should be able to switch that off for fields, that we do not care about when empty.

fancsali avatar Oct 09 '19 15:10 fancsali