1

I have a VF page, on which there is a output field binds to an action support function like below:

<apex:inputField value="{!job.Account__c}" id="AccountInput" >
     <apex:actionSupport action="{!changeAccount}"  event="onchange"  rerender="sectionA"/>                                         
</apex:inputField>
  .....

<apex:inputField id="CustomerInput" value="{!job.Customer__c}" required="true" >
    <apex:actionSupport action="{!changeJobCustomer}" event="onchange"  rerender="sectionB"/>                                           
 </apex:inputField> 

Apex code below:

 public Account account {get; set;}
 public void  changeAccount(){
    if(job.Account__c != null){
        account = [select  Phone, Id , Name from Account WHERE Id = :job.Account__c ];
     } else{
        account = null;
    }
}

Here is the problem, if field 'CustomerAccountInput' is empty(when an validation error happens), the linked function "changeAccount()" does not get triggered. I tried to add Immediate = "true" to the Account action support, then the "changeAccount()" got invoked and validation is ignored, however, since it is immediate "job.Account__c" does not updated with the changed value.

Any body encounter similar problem and have any solution?

P.S. job.Account__c is a look up field in the VF page

  • 1
    I'm not saying it's a duplicate but go check answers to salesforce.stackexchange.com/questions/22213/…. I'm biased of course but looks like the usage of actionregion like in my answer would allow the "partial form submission". Or try splitting them into separate apex:form tags (not a best practice but it now it's only frowned upon whereas in the past it used to significantly blow viewstate size). – eyescream Feb 4 '14 at 22:06
  • 1
    wrap around the first "inputField"'s belonging "page block" within an "actionRegion" sends only partial of the from(without the field required for validation) solve my issue!! And it reduces the viewstate size! :D Thank you! – bluecat Feb 4 '14 at 23:26
  • In that case I've voted after all ;) Glad I could help! – eyescream Feb 5 '14 at 5:45
5

Without seeing your whole VisualForce page its hard to know if this solution will work, but you can use the apex:actionRegion tag to determine which areas of the page are processed by the server when an action is fired.

If you surround everything except the field that is giving you the validation error in apex:actionRegion tags you should be able to achieve your desired result as when your action fires it will no longer submit this field to the server and so it will not try and validate it.

You do not need to have immediate = true for this to work.

The documentation here has more information.

apex:action region

An area of a Visualforce page that demarcates which components should be processed by the Force.com server when an AJAX request is generated. Only the components in the body of the are processed by the server, thereby increasing the performance of the page.

| improve this answer | |
0

The rerender attribute in Visualforce is case sensitive. I noticed you have two different cases for sectionA and SectionB. Verify that the rerender attribute and the id for "sectionA" are the same lower / upper case.

If in fact, they are the same case, then the next step is to debug the changeAccount method to see if the method is getting called and go from there. Hope that helps.

| improve this answer | |
  • sorry it's my bad. I change the typo, the rerender is correct but the function is not called due to validation error on "CustomerInput" – bluecat Feb 4 '14 at 18:44
0

You are correct. Your choices when using required="true" on an apex:inputField are either to let the built in validation code stop your controller method from being called, or to use immediate=true and get no data passed back to the controller; unless you are already passing data via AJAX, I find immediate=true is really only useful for cancellation-type methods.

If you need to do your own validation in your action method, you can remove required="true", and perform any needed validations in you controller. You can use ApexPages.currentPage().addMessage() and <apex:pageMessages> to display errors to the user, just be sure to include your pageMessages in the rerendered portion of the page.

If you want red "required" indicator, you can reproduce that using a little CSS; try using the web inspector in your browser to see how it is rendered by the platform.

| improve this answer | |

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.