3

Very simple example when I use function inside 'required' that should run on actual form validation but it also does execute on load of the page.

The question is how to avoid it and make so it would call other function inside required only on actual validation.

$("form").validate({           
            rules : {
                testinput: {
                    required: runFunction('hello world')
                }
});

function runFunction(a){
    console.log(a);
}
Sparky
  • 98,165
  • 25
  • 199
  • 285
devjs11
  • 1,898
  • 7
  • 43
  • 73

3 Answers3

1

You need to wrap your function call inside another function:

$("form").validate({           
    rules : {
        testinput: {
            required: function(el) {
                runFunction('hello world')
            }
        }
    });
});

The reason for this is because the value returned from runFunction is being set as the value of the required property on load. With the above code you are assigning a function to the required property which will only be run when validating.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • I see, in that case do you think I could avoid adding extra function for each input and just validate a group of them like: $("input[name=testinput],input[name=testinput2],input[name=testinput3]").each(function(){ $(this).rules("add", { required: function(element){runFunction('hello world')} }); }); – devjs11 Oct 22 '13 at 12:03
  • That method should work too, although I don't see the issue with keeping the rules all together in the `validate()` initialisation. – Rory McCrossan Oct 22 '13 at 12:08
  • Thank you, the only issue is that I have way too many input and that would look like: testinput:{required: function(el) {runFunction('hello world')}} testinput2:{required: function(el) {runFunction('hello world')}} testinput3:{required: function(el) {runFunction('hello world')}} testinput4:{required: function(el) {runFunction('hello world')}} etc. – devjs11 Oct 22 '13 at 12:10
  • Is the variable passed in (the `hello world` in your example) different for each input? If not you could simply do `testinput: { required: runFunction }` and then declare the variable within the function. – Rory McCrossan Oct 22 '13 at 12:12
  • ok thank you, last question just as you might know. Why the console always returns 'HELLO WORLD' twice? is this something the plugin does? – devjs11 Oct 22 '13 at 12:14
  • That one I don't know. Is that while using the code in my example? – Rory McCrossan Oct 22 '13 at 12:17
1

Call function in required callback like,

$("form").validate({           
    rules : {
        testinput: {
             required: function(){ runFunction('hello world'); 
        }
    }
});

Read required-method

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
1

another way would be using some sort of partial application

see maybe: Partial Application - Eloquent Javascript

your code might then be something among this lines:

$("form").validate({           
    rules : {
        testinput: {
            required: partial(runFunction,'hello world')
        }
});

Where partial(runFunction,'hello world') creates a new function which is eqivalent to runFunction('hello world').

This a powerfull concept from functional programming, and JS can be extended to support such a thing.

EDIT: 1 might be a better explanation of partial application

http://www.drdobbs.com/open-source/currying-and-partial-functions-in-javasc/231001821

Community
  • 1
  • 1
birdspider
  • 3,034
  • 1
  • 16
  • 25