1000hz / bootstrap-validator

A user-friendly HTML5 form validation jQuery plugin for Bootstrap 3
http://1000hz.github.io/bootstrap-validator
MIT License
2.38k stars 1.07k forks source link

How to custom validate without adding required attribute to the field #635

Open Guervyl opened 6 years ago

Guervyl commented 6 years ago

Hi, I would like to know how to custom validate without adding required attribute to the field. Because having <input data-equals="hello"/> and $('#myForm').validator({ custom: { equals: functions(el$){ ... } } })

does not work. but adding a required to the field execute the custom validations. How to validate a field wich is not required?

heyhusen commented 5 years ago

I got this from documentation:

You can override this value from within your code if you need to change this default behavior. Alternatively, you can add data-validate="true" / data-validate="false" to a specific input to force its inclusion / exclusion in the set of validated fields.

andreaskundig commented 5 years ago

Ignoring custom validators for empty fields that are not required was deliberately added because someone considered a problem: https://github.com/1000hz/bootstrap-validator/issues/214

I also find it unfortunate.

fancsali commented 4 years ago

Well, this is the second time within the past few months I ran into this problem. Could we perhaps come up with a way, to selectively force a validator to run on empty input fields too?

We could add a data attribute to the field, like data-force-validators="this, that".

Or just as data-validatorname=... runs the validator only on non-empty, some other variation (e.g. data-validatorname-force=...) would always run it.

fancsali commented 4 years ago

Ok, I know this is sort of my pet peeve, but I think I've ran into this again, for the third time. I am trying to do complex validation based on different checkboxes and whenever I remove a checkbox the validator fails to run.

So now, I believe the default should be to validate optional fields too, and let developers switch this feature off, by providing some sort of data attribute. (Please see issue #334 as well.)