1

I have a form and each of its elements have their own custom rules working just fine, but I have a situation that the form can't be submmited without providing values for at least one of its elements.

I think this is a rule for the whole form and not for one specific element. I wrote a method that receives all the IDs of the form elements and checkes if they have values and attach it to a element of the form that is not passed as argument to this method, but I didn't think this is the best way to do it.

Any suggestions?

EDIT: As requested here's the method:

$.validator.addMethod("atLeastOne", function(value, element, params) {
    if (!value) return false;
    $.each(params, function(i, param) {
        if (!$(param).val()) return false;
    }
};

And in my form:

$("form").validate(
    rules: {
        field1: { atLeastOne: ["#field2","#field3","#field4","#field5","#field6"] }
});
Philippe Gioseffi
  • 1,488
  • 3
  • 24
  • 41

1 Answers1

1

As far as attaching a rule to the form element using this plugin, that is impossible.

As far as making one field required out of a group of fields, use the require_from_group method contained in the additional-methods.js file.

$("form").validate(
    rules: {
        field1: {
            require_from_group: [1, '.myGroupClass']
        },
        field2: {
            require_from_group: [1, '.myGroupClass']
        },
        field3: {
            require_from_group: [1, '.myGroupClass']
        }
    }
});

The first parameter is how many fields from the group are required. The second parameter does not have to be a class, but it needs to be a valid jQuery selector that targets only the fields in your group.

NOTE: You must use version 1.11.1 of both the plugin and the additional-methods.js file where the require_from_group bug was finally fixed.

Sparky
  • 98,165
  • 25
  • 199
  • 285
  • Actually, this did not work, I'll try to use the bugfix listed in https://github.com/jzaefferer/jquery-validation/issues/412. Are there any chances that this is not working because my fields already have other rules attached to them? – Philippe Gioseffi Jan 13 '14 at 16:24
  • @Philippe, You **must** use version 1.11.1 of both the plugin and the `additional-methods.js` file where the bug was finally fixed. – Sparky Jan 13 '14 at 16:28
  • Now it is working! The problem with yor answer is that you wrote "required_from_group", but the correct name of the method is "require_from_group", with no "d". Thanks again for your time and help! – Philippe Gioseffi Jan 13 '14 at 16:28
  • @Philippe, sorry about that. That's what happens when I rush. Fixed. – Sparky Jan 13 '14 at 16:29
  • No problem at all. Let me ask you one more question: you suggested // for your custom message jQuery.extend(jQuery.validator.messages, { require_from_group: jQuery.format("'Please enter either username/ email address to recover password'/Please fill out at least {0} of these fields.") }); in your answer in http://stackoverflow.com/questions/15136943/jquery-validate-out-of-two-blank-fields-at-least-one-field-must-be-filled-or-b, isn't this making this answer as default for all the pages that uses this method? – Philippe Gioseffi Jan 13 '14 at 16:32
  • @Philippe, it's JavaScript so it can only affect the page it's loaded on. – Sparky Jan 13 '14 at 18:35
  • True, forgot about that. Thanks! – Philippe Gioseffi Jan 13 '14 at 20:58
  • @PhilippeGioseffi, `require_from_group` has nothing do with `skip_or_fill_minimum`. The `require_from_group` bug was already fixed in 1.11.1, unless you have new information that nobody else knows about. – Sparky Feb 07 '14 at 16:21
  • @PhilippeGioseffi, any bug complaints on SO about `require_from_group` always clear up when the version is updated to 1.11.1. I am going to defer to the developer and the many months he spent working with all the people on SO and GitHub fixing `require_from_group` for version 1.11.1. – Sparky Feb 07 '14 at 16:29
  • I read people confirming that in *1.11.1* the bug is fixed, I myself was one of them, but since the `skip_or_fill_minimun` bug still exists and it was very much the same of `require_from_group` and they both were corrected in the same commit I think that may still be a problem. – Philippe Gioseffi Feb 07 '14 at 16:34
  • @PhilippeGioseffi, maybe. Although we're not seeing any SO complaints about it either. Not yet at least. – Sparky Feb 07 '14 at 16:39
  • I'll wait for your contact with the developer. Let me know about it, please. – Philippe Gioseffi Feb 07 '14 at 16:45