Pristine icon indicating copy to clipboard operation
Pristine copied to clipboard

How to Ignore hidden elements

Open dhruvangg opened this issue 4 years ago • 6 comments

I have dependent fields in the form. How to ignore hidden fields from validation.

dhruvangg avatar Aug 18 '20 09:08 dhruvangg

Hidden fields are ignored by default. Are you seeing a different behavior?

sha256 avatar Aug 18 '20 11:08 sha256

Yes, Depentdent fields not ignored. Check this fiddle. https://jsfiddle.net/Dhruvang/pvnheqy2/4/

When you select Yes in first question, second question will be hide. But it is not ignored.

dhruvangg avatar Aug 18 '20 12:08 dhruvangg

@dhruvangg, only input type=hidden fields are ignored. It does not look for hidden elements (I'm not sure if it should at this moment). But there's one way to solve the issue in your case: remove the required attribute from the dependant attribute and re-initialize the Pristine object. In any case, you have to re-initialize the Pristine object if any attribute is modified.

sha256 avatar Sep 02 '20 09:09 sha256

Hi, can you add some function for re-initialize for example reset()

1ucay avatar Sep 05 '20 08:09 1ucay

@1ucay, you can basically do the following to re-initialize:

pristine.reset(); // resets errors
pristine.destroy(); // clean up
pristine = new Pristine(form); // re-init

sha256 avatar Sep 06 '20 13:09 sha256

If you have dependent/dynamic fields in a form, than try using combo of hidden attribute with disabled. Than, you can pass something like this

const pristine = new Pristine(form)
const activeInputs = (form) => [...form.elements].filter(input => !input.disabled && input.type !== "hidden"))
pristine.validate(activeInputs(form))

essentin avatar Dec 16 '20 15:12 essentin