Parsley.js
Parsley.js copied to clipboard
Request: a way to validate multiple fields as one
I've been looking for a way to do a validation-check on multiple fields, obviously for a date-input that consists of three inputfields, but couldn't find much except for this somewhat hackish solution:
http://stackoverflow.com/questions/27383263/validate-multiple-fields-with-parsley
Is there any way to imlpement this correctly at the moment?
The short answer is "not well". It would be awesome do have a good way to deal with interdependencies.
:+1:
I also would love to see something like this. I think it's pretty important thing to have.
I would also love to see this, my use case is IBAN validation composed of 7 fields.
It will be my focus as soon as I finish the work on promises, I promise :smile:
@marcandre thanks!
Promises: :white_check_mark:
Next: interdependencies.
One possible approach: #949
I'm not 100% convinced, though. Seems likes sometimes, the validation of foo
depend on bar
. If it fails, we want to display the error in foo
. Sometimes, there's a "global" validation that depends on foo
and bar
, and maybe the error need to be displayed somewhere else than errors strictly on foo
and bar
.
I think http://verifyjs.jpillora.com/#groups describes a nice way of declaring such rules.
Has a solution been made for this? Question was asked in 2015 and now we're nearing 2019
@DDChiang I've not around to it, or anyone else for that matter...
Do check this example that can work very well (at least for some use cases)
I am using a multistep form as shown in the other example so it seems that prevents me from having different group names in one step.
I imagine I might be able to achieve it if the group arg could take a reg-exp then I could have the followoing groups in step 1: 'group-1-1', 'group-1-2', 'group-1-3'
I am using a multistep form as shown in the other example so it seems that prevents me from having different group names in one step.
I imagine I might be able to achieve it if the group arg could take a reg-exp then I could have the followoing groups in step 1: 'group-1-1', 'group-1-2', 'group-1-3'
The group
option can be an array, and I'd accept a PR / sponsorship so that a regexp is also acceptable (internally only _isInGroup
need to be modified).
@marcandre an array is all I needed, thanks! bad asuumption on my part, should have checked the source code.
But actually, in the end, I solved it another way by just using jquery find to get the divs input(select in this case) elements.
Parsley.addValidator('oneChildEquals', {
requirementType: ['string', 'string'],
validateString: function(_value, requirement, requirement2, instance) {
var $inputs = $(instance.element).find("select.language-proficiency");
var valid = false;
$inputs.each(function(i){
if($(this).val() == requirement){
valid = true; // one input has the target value (requirement2)
return false; //break out of the loop
}
});
// no input has the target value (requirement2)
return valid;
},
messages: {en: 'You must choose at least one language with %s'},
});