guillaumepotier / Parsley.js

Validate your forms, frontend, without writing a single line of javascript
http://parsleyjs.org
MIT License
9.05k stars 1.32k forks source link

Request: a way to validate multiple fields as one #839

Open petervdn opened 9 years ago

petervdn commented 9 years ago

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?

marcandre commented 9 years ago

The short answer is "not well". It would be awesome do have a good way to deal with interdependencies.

chibicode commented 9 years ago

:+1:

custa1200 commented 9 years ago

I also would love to see something like this. I think it's pretty important thing to have.

remydavid commented 8 years ago

I would also love to see this, my use case is IBAN validation composed of 7 fields.

marcandre commented 8 years ago

It will be my focus as soon as I finish the work on promises, I promise :smile:

chibicode commented 8 years ago

@marcandre thanks!

marcandre commented 8 years ago

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.

torotil commented 8 years ago

I think http://verifyjs.jpillora.com/#groups describes a nice way of declaring such rules.

DDChiang commented 5 years ago

Has a solution been made for this? Question was asked in 2015 and now we're nearing 2019

marcandre commented 5 years ago

@DDChiang I've not around to it, or anyone else for that matter...

marcandre commented 5 years ago

Do check this example that can work very well (at least for some use cases)

ryan2johnson9 commented 4 years ago

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'

marcandre commented 4 years ago

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).

ryan2johnson9 commented 4 years ago

@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'},
      });