-2

Does anyone have an idea or is it even possible(I'm sure it is) to get some rules from other variable? If there are like "global" rules that some validators use such as

var validationRules = {
    name: {required:true},
    age: {required:true}
};

And then I would have validator

   $("form").validate({
        rules: {
            somemorerules: {required:true },
            manymanyrules: {required:true },
            //Now i would like to import name and age from var validationRules.
   });

Could anyone help or give me some tip to get going. I have spent a couple of hours in this now.

Sparky
  • 98,165
  • 25
  • 199
  • 285
  • See options 1a and 1b in [this answer](http://stackoverflow.com/a/9056425/594235). Also see [this answer](http://stackoverflow.com/a/9461732/594235). – Sparky Sep 20 '16 at 13:20
  • Sorry i really didnt find anything, i think i googled it over 2hours. Thanks ! – Alan Aasmaa Sep 21 '16 at 06:15

1 Answers1

1

You can combine two objects in one by using Object.assign() method.. so you can pass one default validation rule object and one is your custom validation rule object.

var validationRules = {
    name: {required:true},
    age: {required:true}
};
 $("form").validate({
        rules: Object.assign({
                somemorerules: {required:true },
                manymanyrules: {required:true }
              },validationRules)

   });

Refer Object.assign()

Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42