2

I'm having a problem. Up till' now, I was using my own jQuery build to do this. Meanwhile, I was told to change it to jQuery validation plugin. It was working before but now I've been stuck in trying to make the action of the command execute and rerender (it doesn't even rerender at the moment). The field validation work fine, just the submit is messing me all up. I'd appreciate any help or advice you could give.

        function validateForm(id,e){

        var form =  jQuery('[id$='+id+']');

        form.validate();             

        jQuery('[id$=name]').rules("add",{
            required: true,
            minlength: 5
        });
        jQuery('[class$=costPercentage]').rules("add",{
            required: true,
            number: true
        });

        jQuery.validator.messages.required = "You better have entered a value.. or else!";
        jQuery.validator.messages.equalTo = "No silly, you're supposed to type the same set of characters AGAIN.";
        if(!form.valid()){
             e.preventDefault();
             return false;

        }    
        return true;
    }

<apex:commandButton value="Save" onclick="return validateForm('SomeId',event);" action="{!createFunction}" rerender="SomeForm"/>
  • What do you mean when you say the submit 'is messing you all up'? Are you getting an error? What is working and not working? – Chris Duncombe Oct 30 '13 at 15:39
  • When I click on Save, the whole page simply reloads without doing anything, it just shows the same form with the same values I had in the inputfields. – Rui Rocha Oct 30 '13 at 15:43
  • I don't believe you should be returning anything in your validate function. Try to take out the return true and add the return false to your onclick call in the button. – Chris Duncombe Oct 30 '13 at 15:54
  • Thanks for the tip but it still does the same. I ends up reloading the entire page without doing anything, showing everything the same from the moment before I clicked Save. – Rui Rocha Oct 30 '13 at 16:05
  • Try : onclick="if (validateForm('SomeId',event) == false) return false; – Cloud Ninja Oct 30 '13 at 19:40
2

How about using an <apex:actionFunction> instead of using the action parameter?

<apex:actionFunction name="doCreate" action="{!createFunction}" rerender="SomeForm"/>

snip

  // on passing validation
  doCreate();
}

snip

<apex:commandButton value="Save" onclick="return validateForm('SomeId',event);"/>
| improve this answer | |
  • 1
    I concur with LaceySnr, I find it better not to mix two approaches, if I'm calling the logic from JS I just continue on and use actionFunction to call the post Apex side of things from JS too. – Steven Herod Oct 31 '13 at 1:59
  • Sorry for only replying now, but this is a possible solution, it worked! Thank you. – Rui Rocha Nov 4 '13 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.