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
venkatasubhashkvenkatasubhashk 

Display Custom Validation Error on Visualforce page

I have a Visualforce page which uses standard controller and controller Extensions, I want to dispaly a Custom Validation Error message on the VF page. Any ideas!

bob_buzzardbob_buzzard

Assuming you are looking to generate an error from your controller, you can use the following.

 

In the controller:

 

 

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

 

 

and in the page:

 

 

<apex:pageMessages/>

 

If that isn't what you are looking for, can you explain a bit more about your scenario?

 

 

 

venkatasubhashkvenkatasubhashk

 

The Following is my controller Extensions and vf page ,i have custom validation rule on the field quantity__c ,when the page is saved can i get the validation error displayed on VF page ....
public class DEV_MULTIPLE_InvoiceLineEntry {
    
    }
  }
    public List<Invoice_Line_Item__c> ords {get; set;}
    private final Invoice__c parOrd;
    public DEV_MULTIPLE_InvoiceLineEntry(ApexPages.StandardController myController) {
        parOrd=(Invoice__c)myController.getrecord();
       
        ords = new List<Invoice_Line_Item__c>();
         // Get the Current User
     user u1 =[select id ,name,Formula_Warehouse__c from user where id=:UserInfo.getUserId()];
         //Get The DefaultwareHouse for the user
    
     invoice__c pa =[select id,name, doctor__c from Invoice__c where id=:parOrd.id];
     
  
      
        LitOrd.Invoice_Number__c = parOrd.id;
        LitOrd.staff__c = pa.Doctor__c;
       
       
        ords.add(LitOrd);
         
        } 
        
        
       
    public void addrow() {
    
      
     
     invoice__c pa =[select id,name, doctor__c from Invoice__c where id=:parOrd.id];
      
        Invoice_Line_Item__c LitOrd = new Invoice_Line_Item__c();
        LitOrd.Invoice_Number__c = parOrd.id;
        
        LitOrd.staff__c = pa.Doctor__c;
       
       
        ords.add(LitOrd);
        }
      
        
        }   
           
        }  
           
            
    public void removerow(){
        Integer i = ords.size();
        ords.remove(i-1);}
            
       public PageReference save() {
       
        insert ords;
      
        return(new ApexPages.StandardController(parOrd)).view();
       
         }
        
        
        }
//////vf page/////
<apex:page standardController="invoice__c" extensions="DEV_MULTIPLE_InvoiceLineEntry">
<apex:pageMessages/>
    <apex:form >
    <apex:pageBlock title="Add Invoice Line Items" >
            <apex:pageBlockButtons >
            
                <apex:commandButton value="Save" action="{!save}" rerender="error" />
                <apex:commandButton value="Cancel" action="{!cancel}" rerender="error" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!ords}" var="a" id="table">
                <apex:column headerValue="Item">
                    <apex:inputField value="{!a.Item__c}"/>
                </apex:column>  
                              
                 <apex:column headerValue="Quantity">
                    <apex:inputField value="{!a.Quantity__c}"/>
                   
                </apex:column>
                
                 <apex:column headerValue="Remarks">
                    <apex:inputField value="{!a.Remarks__c}"/>
                </apex:column>
                
                <apex:column headerValue="Item Warehouse">
                    <apex:inputField value="{!a.Item_Warehouse__c}"/>
                </apex:column>
                
                <apex:column headerValue="Selling Price">
                    <apex:inputField value="{!a.Selling_Price__c}"/>
                </apex:column>
                
               <apex:column headerValue="Staff">
                    <apex:inputField value="{!a.Staff__c}"/>
                </apex:column>
                
                
                
                
                
            </apex:pageBlockTable>
    <apex:pageblockButtons location="bottom">
        <div style="text-align:right;margin-right:30px;font-weight:bold;">
            <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error" immediate="true" />
&nbsp;|&nbsp;&nbsp;
            <apex:commandLink value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />                
        </div>
    </apex:pageblockButtons>  
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

bob_buzzardbob_buzzard

So do you have a regular validation rule on the quantity field?  What behaviour are you looking for?  If you are looking for the same behaviour as the standard pages (e.g. an error message displayed beneath the input field) I think you may need to code this yourself.  I've found that validation rules in visualforce give a lovely long message referring to the full id of the component that failed the rule.

venkatasubhashkvenkatasubhashk

Hi,

yes you are right !! 

 

i have the standard validation rule for quantity.(i will ignore this)

 

please help me how can i add error message through controller also !

bob_buzzardbob_buzzard

This won't be a simple fix unfortunately.  I think you'll need to create a wrapper class that wraps up an Invoice_Item__c with an error message per field.  Then when you are repeating through the list, you will need to check if there is an error associated with the quantity and render the error message out at that point.

venkatasubhashkvenkatasubhashk

ok 

 

Thank you , I will Try that !

 

so i cannot use  the following either ? in controller and VF page ?

 

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

 

<apex:pageMessages/>

bob_buzzardbob_buzzard

You can do that, but it will put an error message out wherever pagemessages happens to be - it won't tie in with the element in the list that has failed validation (unless you put out the row number or similar).

Nisar AhmedNisar Ahmed

Hi Bob,

 

I have a similar kind of problem. 

I need to display an custom validation error message for an 'inputText' field in the page itself i.e., by using Javascript.

For Example:

Here, I need to display an error message if the inputText for 'Name' is null or empty.

 

<div style="position:absolute; top: 145px; left: 30px;" class="cnt_popup">
Name: 
</div>
<div style="position:absolute; top: 140px; left: 105px; height:20px; width:200px;" >
     <apex:inputText id="name" value="{!Name}" styleClass="textarea" required="true">                                    
     </apex:inputText>  
</div>

 Please let me know if you have any idea. Thanks.