Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery validation for visualforce for apex:commandbutton input type='button' ajax call
    primarykey
    data
    text
    <p>I am trying to jQuery validation piece for an <code>apex:commandbutton</code> in visualforce (Salesforce). It's working fine if its not an AJAX call, I mean if dont set the <code>oncomplete</code> and rerender properties for the button. But if I do so, it makes the input <code>type='button'</code> instead of <code>'submit'</code>. So whatever method I have specified in the action attribute of the <code>apex:commandbutton</code> is getting called irrespective of my validation result (<code>true</code> or <code>false</code>).</p> <p>To do that I tried the following options and nothing worked out.</p> <p>This is what I want :</p> <ol> <li>Fill the form fields</li> <li>On clicking the command button, it should validate using jQuery (as of now it's working if its type is submit but unfortunately if i change it to AJAX call it becomes <code>type='button'</code> and not working)</li> <li>call my action method <code>action="{!SaveUserAndSendEmail}"</code> (if validation passes creates the contact record)</li> <li><code>Oncomplete="clickCreateUserButton();"</code> (this will create the user record once the contact record is created by action method.</li> </ol> <p>Options I tried:</p> <ul> <li><strong>Method 1</strong>:</li> </ul> <p><strong>a.</strong> Adding the following JQuery Script inside <code>document.ready()</code>:</p> <pre><code>j$('[id$=SaveUserAndSendEmail]').click( function() { if (j$('[id$=theForm]').validate().form() == true) { isValid = true; SetContinueProcess(true); //callSaveUserAndSendEmailHidden(); //j$('[id$=SaveUserAndSendEmailHidden]').click(); return true; } else { isValid = false; SetContinueProcess(false); return false; } } ); </code></pre> <p><strong>b.</strong> As you can see I have called a function called setContinueProcess which calls the actionsupport function and sets a property ContinueProcess and if its false am blocking the action inside the action method in controller.</p> <p>This didn't work because it goes to server side and clears the validation messages.</p> <ul> <li><strong>Method 2</strong> :</li> </ul> <p><strong>a.</strong> Same Jquery method as mentioned above. Only change is that instead of calling the setContinueProcess, I tried having another button and made it hidden using display:none and tried clicking it from within the above if condition both using jquery and normal javascript <code>document.getelementbyid("buttonid").click()</code>; this called the action method and created the contact record but oncomplete method was not called.</p> <ul> <li><strong>Method 3</strong>:</li> </ul> <p><strong>a.</strong> Same Jquery method, instead of hiddenbutton, called an ActionSupport function and specified its action method in its action atttribute and oncomplete attribute as i did for the button. Even it called only the action method and did not call the oncomplete method</p> <ul> <li><strong>Method 4</strong>:</li> </ul> <p><strong>a.</strong> Same jquery method, instead of calling oncomplete method in actionsupport, called both the action support method and oncomplete method from within the above if condition itself :- </p> <pre><code>SaveandSend(); //Actionsupport method name which calls the controller's action method and creates the contact CreateUser(); // Javascript method which clicks another button triggering the action method of that to create the user </code></pre> <p>even in this one, the second method didn't get called.</p> <p>JavaScript Code:</p> <pre><code> function callSimulateUserSave() { var mybtn = document.getElementById('{!$Component.theForm.SimulateUserSave}'); mybtn.click(); } </code></pre> <p>Visualforce Code:</p> <pre><code>&lt;apex:actionFunction name="SetContinueProcess"&gt; &lt;apex:param name="ContinueProcess" value="ArgValue" assignTo="{!ContinueProcess}" /&gt; &lt;/apex:actionFunction&gt; --commented -- &lt;apex:actionFunction name="SaveandSend" action="{!SaveUserandSendEmail}" &gt; &lt;/apex:actionFunction&gt; -- commented -- &lt;apex:commandButton id="SaveUserAndSendEmail" value="Save and Send Activation Email" action="{!SaveUserandSendEmail}" rerender="junkpanel" oncomplete="if (j$('[id$=theForm]').validate().form() == true){callSimulateUserSave();}"&gt; &lt;apex:commandButton value="SimulateUserSave" id="SimulateUserSave" action="{!CreateUserRecord}" style="display:none;margin-left:5px;"/&gt; &lt;apex:commandButton id="SaveUserAndSendEmailHidden" style="display:none;" value="Save and Send Activation Email" action="{!SaveUserandSendEmail}" rerender="junkpanel" oncomplete="callSimulateUserSave()" &gt; &lt;/apex:commandButton&gt; </code></pre> <p>-- I might have commented out some code, those are the ones I tried for different methods/approaches I tried to achieve the solution</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload