I would like to validate hidden fields, so essentially I want to remove input[type=hidden]
from parsley's list of excluded form elements. I've tried explicitly setting excluded elements in parsley's options, but hidden fields are still not validated. E.g:
$(element).parsley({
excluded: 'input[type=button], input[type=submit]'
});
Any thoughts on how to accomplish this, or what I'm doing wrong?
I had the same problem, yet even when I set up the global configuration it was not working. The problem essentially, was that I was redefining the options.
Global config:
In my code I had to dynamically change the inputs. Then, I set them to the default:
Which I had to change to
To takeaway: If you change the config, you have to override all the settings.
This is definitely a bug in parsley, in fact someone has already opened an issue regarding it, see: https://github.com/guillaumepotier/Parsley.js/issues/664, but unfortunately nothing has been done yet. Basically, after analyzing the source code of parsley the issue is that the options of the parsleyFormInstance are being ignored if the field is already being excluded globally. There is the following if statement deciding this:
In order to solve this, I have added the following line of code:
exactly before the OptionsFactory is initialized on line 2160
I'm guessing this is a bug.
Take this form:
With the following javascript, even though we tell Parsley to validate hidden fields, it does not work:
However, if you define this as a global configuration, it does work
what i did was: make input field readonly and hide via css.
You can use the method of excluding hidden field when initializing parsley. But it comes with a downside for some scenarios where you have to dynamically hide/un-hide sections of form. Another alternative method is to override the "validated" event, and check inside if the field is hidden or not.
See the below code snippet: