I have an <apex:form> which has search action and upsert action, the thing is that I want the fields inside this form to be required just when I click on insert button.

Images Action

I tried to make these fields required from the object page, but then they become required even in search action.

Is there anyway could help me making these fields required just for Add button.


3 Answers 3


You have to add these fields in <apex:pageBlock> section and then you have to make the field required, it will work!


<apex:page standardController="MyObject__c"  extensions="MyObjectController" showHeader="true" sidebar="true">
<apex:pageblock mode="edit" title="Search Serials" >
<apex:pageMessages />
<apex:pageblockbuttons > 
    <!-- add/remove other css classes for button look and feel -->
    <input type="Add" value="Continue"  class="btn" onclick="callActionFunction();"/>
    <input type="Add" value="Search"  class="btn" onclick="callActionFunction1();"/>
    <input type="Add" value="Reset"  class="btn" onclick="callActionFunction2();"/>
 <apex:outputpanel >
    <apex:pageblocksection title="Search Serials" showheader="true" columns="2" >
        <apex:inputfield id="type" value="{!MyObject__c.Type__c}" required="true" />
        <apex:inputfield id="fieldName" value="{!MyObject__c.prifix__c}" required="true"/>
        <apex:inputfield id="Start" value="{!MyObject__c.start__c}"/>
        <!-- so on... -->

If you like to make the fields required only for specific actions then either validate on client side by Javascript/JQuery, or server side - custom logic in your action in your controller/extension. The standard visualforce tag parameter required will make the field required at all times when you try to submit the form or call a method in the controller.


Thanks to @anmrk, I read this article and it was very helpful.

I solved it by validating the fields in the APEX controller when I do the upseart operation, So the fields actually aren't required but in the upsert function if there's any field that equals null I display a message.

Otherwise, I didn't use this validation in the search operation so it went will.

public void update_serial(){
  if(serial.Type__c != null && serial.Year__c != null && serial.Current__c != null .....){
      upsert serial;
      serial = new Serial__c();
    }catch(Exception e){
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, e.getmessage() ));
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'All fields are required !!' ));


And about the visualforce page, I used a stylesheet to make the fields looks required.

  • Any reason you didn't do this in a validation rule instead? You can validate on insert only in validation rules.
    – pchittum
    Sep 7, 2014 at 0:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .