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
BerettaJonBerettaJon 

Validation rules on VisualForce page.

Hi all,

 

I am trying to build a VisualForce page that would check certain fields for data validity before inserting.  I believe I want to use a validation rule for this.

 

How am I able to mimic the standard functionality of a validation error message appearing below the associated field?

 

Currently the insert statement in my apex controller throws a DML error referencing the validation rule I have set up for the first field that needs one.  This error does not go underneath the field as I told it to do in the validation rule, it redirects to a seemingly new page that is 100% comprised of the DML error.  How can I handle this properly, or rather make it behave like the standard intended functionality?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
TejTej

add this validation in your controller by replacing myObject.Field with your variables and add<apex:pagemessages> tag in your visualforce.

 

so the error will be shown below that particular field in your page

 

if ( myObject.Field == null || myObject.Field == '' ) {

myObject.Field.addError( 'You must enter a value');

}

All Answers

TejTej

add this validation in your controller by replacing myObject.Field with your variables and add<apex:pagemessages> tag in your visualforce.

 

so the error will be shown below that particular field in your page

 

if ( myObject.Field == null || myObject.Field == '' ) {

myObject.Field.addError( 'You must enter a value');

}

This was selected as the best answer
BerettaJonBerettaJon

Thanks this was the part I was missing in my understanding.

 

myObject.Field.addError( 'You must enter a value');

 

I take it back.  it was the pagemessages VF tag that did the trick.  Now my page is using the error message given in the validation rule itself.

TejTej

inorder for the error to appear below the field name in your page, you have to use that syntax.

 

myObject.field is nothing but --Lead.firstname or  Account.Name.

 

And the message you want to show will go inside the paranthesis

BerettaJonBerettaJon

Yeah I get it now.  I was unaware of the error attribute on fields.  It then makes sense that salesforce magic would happen if it sees a field with an error.

 

I take it back.  It was the apex pagemessages tag that did the trick.  Even more salesforce magic, it is using the error message from the validation rule like I thought it would in the first place.

SFDCDevQASFDCDevQA

I am running into a similar issue.  Can you clarify how the page messages tag helped?  Where did you put it?  Did you put a tag for each field you have a validation rule?  Did you still need to do

if ( myObject.Field == null || myObject.Field == '' ) {

myObject.Field.addError( 'You must enter a value');

}

as Tej recommended?

 

Thanks,

Amanda