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
Mahesh Tandan 6Mahesh Tandan 6 

does validation require in both page and controller?

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.


 
Best Answer chosen by Mahesh Tandan 6
Ashish Singh SFDCAshish Singh SFDC
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.

All Answers

VinayVinay (Salesforce Developers) 
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
Ashish Singh SFDCAshish Singh SFDC
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.
This was selected as the best answer
Mahesh Tandan 6Mahesh Tandan 6

Well Thank you Ashish Singh sfdc

your answer helped me