1

How can a commandButton ignore validation for one particular required field?

I am working on a seemingly simple portal account page with the following markup:

<apex:page standardController="Contact" extensions="MyController">
    <apex:form>

        <apex:pageBlock>

            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Update"/>
            </apex:pageBlockButtons>

            <apex:pageBlockSection title="Details" columns="2" id="details">
                <apex:inputField value="{!Contact.LastName}" label="Contact Name" required="true"/>
                <apex:commandButton action="{!updateContact}" value="Update Contact" rerender="details"/>
                <apex:inputField value="{!Contact.Account.Name}" label="Account Name" required="true"/>
                <apex:commandButton action="{!updateAccount}" value="Update Account" rerender="details"/>
            </apex:pageBlockSection>

        </apex:pageBlock>

    </apex:form>
</apex:page>

When "Update Contact" is clicked I want the server to only validate the first field. Conversely the "Update Account" button should only validate the second field. When the main "Update" button is clicked I want the entire page to be validated.

I am seeing 2 solutions in front of me but I guess I'm not sure how to utilize them properly:

  • actionRegions
  • multiple forms

The pageBlockButtons wants to be the direct child of the pageBlock. Therefore I cannot wrap it inside a form. Also if I wrap the buttons inside actionRegions that will bypass validation.

4
  • do you use Contact.Account.Name as example contact field, or you want to update account itself? Commented May 1, 2016 at 4:51
  • @IlyaLepesh I am updating the Account itself. Commented May 1, 2016 at 4:53
  • Could you please describe what kind of validation do you need? Only page validation, that field was provided? Or you talking about validation rules, each for object? Commented May 1, 2016 at 5:30
  • @IlyaLepesh page validation Commented May 1, 2016 at 5:34

2 Answers 2

1

I believe wrapping the field-button pairs in actionRegions does the job! That way each button validates its counterpart field. Finally the main page-level button can be left alone to operate normally.

0

I think there will be no problem when you use validation code in controller. Both Command button call different methods. In updateContact() method, write validation of contact and in method updateAccount() write validation of account.

Hope it may helps you.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.