function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
james2000james2000 

required fields enforced on cancel with custom controller

I'm trying to build an edit page that contains a save and cancel button. The page has a required field and also has a custom controller.

The problem is that when I press the cancel button to invoke my cancel action, the cancel action is not invoked and instead the page displays an error that I must enter a value for the required field if I have no text in the field.

Is there a way to have this logic bypassed for a cancel action?


Code:
<apex:page controller="NewCaseCommentController" tabStyle="Case">
  <apex:sectionHeader title="Comments" subtitle="Case {!parentCase.CaseNumber}" help="/help/doc/user_ed.jsp—loc=help&target=case_comments_edit.htm&section=Cases"/>
  <apex:form>
    <apex:pageBlock title="Case Comment Edit" mode="edit">
      <apex:pageBlockButtons>
        <apex:commandButton action="{!save}" value="Save"></apex:commandButton>
        <apex:commandButton action="{!cancel}" value="Cancel"></apex:commandButton>
      </apex:pageBlockButtons>
      <apex:pageBlockSection title="Case Details" columns="1">
        <apex:outputField value="{!parentCase.Subject}"/>
        <apex:outputField value="{!parentCase.Description}"/>
      </apex:pageBlockSection>
      <apex:pageBlockSection title="Case Comments" columns="1">
        <apex:pageBlockSectionItem>
          <apex:outputLabel value="Public" for="comment__ispublished" style="text-align:center;"/>
          <apex:inputCheckbox value="{!comment.IsPublished}" id="comment__ispublished"></apex:inputCheckbox>
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem>
          <apex:outputLabel value="Comment" for="comment__commentbody"/>
          <apex:inputField value="{!comment.CommentBody}" required="true" id="comment_commentbody" style="width:450px; height:135px"/>
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
  <!-- apex:relatedList does not work for CaseComments -->
  <apex:pageBlock title="Case Comments" helpUrl="/help/doc/user_ed.jsp–loc=help&target=case_comments.htm&section=Cases" helpTitle="Case Comments Help">
    <apex:pageBlockList value="{!existingComments}" var="ec">
      <apex:column headerValue="Public" headerStyle="text-align:middle;"><apex:outputField value="{!ec.IsPublished}"/></apex:column>
      <apex:column headerValue="Comment"><b>Created By: &nbsp;<apex:outputLink value="/{!ec.CreatedById}">{!ec.CreatedBy.Name}</apex:outputLink> (<apex:outputField value="{!ec.CreatedDate}"/>)</b><br /><apex:outputField value="{!ec.CommentBody}"/></apex:column>
    </apex:pageBlockList>
  </apex:pageBlock>

</apex:page>

 Thanks

Best Answer chosen by Admin (Salesforce Developers) 
aballardaballard

See the "immediate" attribute for commandButton.

 

All Answers

aballardaballard

See the "immediate" attribute for commandButton.

 

This was selected as the best answer
james2000james2000
Setting immediate = true worked. Thanks