Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
I have a visualforce page and a custom controller

vf page have a form and required fields and i'm validating it throught <apex:inputtext value="{!data}" required="true">

my question is do I need to validate it in my controller also?

like if

if(data == Null || data == ''){

pexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'my error message'));

}

i'm really confused about it.

please help.

 
3 answers
  1. Feb 29, 2020, 9:35 AM
    Hi Mahesh,

    The simple answer is if you've marked it as required on VF page then it will be required field and you need no to handle on controller side.

    But just imagine a situation, where you want to reuse same controller on the other VF page. If you don't want that field to be  as required on that VF page don't use this attribute.

    So, it depends on you're requirement.

    Thanks,

    Ashish Singh.
  2. Feb 28, 2020, 10:23 PM
    Hi Mahesh,

    No you can handle it in VF page.

    For better understanding please execute below code in your org and you can see validation error.

    =======================================================

    <apex:page standardController="Contact">

    <apex:form>

    <apex:messages/>

    <apex:pageBlock title="PageBlock"  mode="edit">

    <apex:pageBlockSection title="Page block section">

      <apex:outputLabel value="Birth date" for="mId"/>

      <apex:outputPanel styleClass="requiredInput" layout="block">

       <apex:outputPanel styleClass="requiredBlock" layout="block"/>

      <apex:inputText value="{!contact.Birthdate}" required="true" id="mId"/>

     </apex:outputPanel>

    </apex:pageBlockSection>

    <apex:pageblockButtons>

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

    </apex:pageblockButtons>

    </apex:pageBlock>

    </apex:form>

    </apex:page>

    ==========================================================

    Hope this helps you.

    Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

    Thanks,

    Vinay Kumar
0/9000