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
dmanidmani 

Custom Error Message for inputtext

Hi

 

i need to display custom error message for a required inputtext. How is it achievable.

 

I have tried

 

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Subject is required'));
return null;

 

in controller method

 

and

 

<apex:pageBlock>

<apex:pageMessages ></apex:pageMessages>

<apex:pageblockSectionItem id="taskid" >
                            <apex:outputLabel value="Subject" />
                            <apex:outputPanel id="panel"  >
                                <div class="requiredInput">
                    <div class="requiredBlock"></div>
                    <apex:inputText label="Subject" value="{!Taskt}" required="true"/>
                </div>
                
                </apex:outputPanel>
                    </apex:pageblockSectionItem>

</apex:pageBlock>

 

  in page.

 

But its not helpful . kindly provide your ideas.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Puja_mfsiPuja_mfsi

Hi,

you have done a small mistake here.

please check your variable null check as :

 

if(Taskt==null || Taskt.length() == 0 ) {

    system.debug('Error validation');
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Subject is required'));
     return null;

}

 

 

Please let me know if u have any problem on same and if this post helps u please give KUDOS by click on star at left.

All Answers

bob_buzzardbob_buzzard

You'll have to handle this server side and decorate the page to make the field look like a required one.

 

I've written a blog post that should give you a heads up on this:

 

http://bobbuzzard.blogspot.co.uk/2011/04/field-level-error-messages-with_29.html

Puja_mfsiPuja_mfsi

Hi,

If you want to display your own message then you don't need to use  required="true" in your inputText.

You need to use server side validation in your code like:

 

if( Taskt == null || Taskt.length() == 0 )  {

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Subject is required'));
return null;

}

 

 

Please let me know if u have any problem and if this post helps you plz give KUDOS by click on star at left.

dmanidmani

hi Puja,

 

I tried the way you mentioned,but when i remove "required"  attribute  the record is getting saved without ay errors.

 

I handled the same validation  u mentioned. But still i am not able to display custom error message. help me pls.

Puja_mfsiPuja_mfsi

Hi,

Can you please post your apex and VF code here.

dmanidmani

VF Page

 

<apex:page Controller="TransferLeads" sidebar="false" id="page">
<apex:pageMessages ></apex:pageMessages>
    <apex:form id="formid">   
 
        <apex:pageBlock title="New Task" id="thePageBlock" mode="edit">
           
          
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!saveTask}"  />
                <apex:commandButton value="Cancel" action="{!cancel}" immediate="true"/>                
            </apex:pageBlockButtons>

            <apex:actionRegion >         
                   <apex:pageBlockSection id="pageBlockSection"  title="Task Information" columns="1">
                       
       
                <apex:inputField value="{!lecord.ownerid}"/ >
       
                        
                    <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Lead Name"/>
                            <apex:OutputField value="{!Lead.Name}"/>
                        </apex:pageBlockSectionItem>
                  
                                               
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Current owner"/>
                            <apex:OutputField value="{!Lead.Owner.Name}"/>
                        </apex:pageBlockSectionItem>
                        
                       <apex:pageblockSectionItem id="taskid" >
                            <apex:outputLabel value="Subject" />
                            <apex:outputPanel id="panel"  >
                                <div class="requiredInput">
                    <div class="requiredBlock"></div>
                    <apex:inputText label="Subject" value="{!Taskt}" required="true"/>
                </div>
                
                </apex:outputPanel>
                    </apex:pageblockSectionItem>
                        
                                               
                        <apex:pageblockSectionItem >
                            <apex:outputLabel value=" Who is Transfering" />
                            <apex:inputField value="{!User1.Name}"/>
                        </apex:pageblockSectionItem>
                        
                       
                  </apex:pageBlockSection>
       </apex:actionRegion>
        </apex:pageBlock>
    </apex:form>
   
</apex:page>

 

 

Controller:

public with sharing class TransferLeads{
public String Taskt{get;set;}
public PageReference saveTask()
{
if(Taskt==null)   
            {
            system.debug('Error validation');
                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Subject is required'));
                 return null;
}

try
{
update Leadrecordlistname;
}
catch (DmlException e) {
              System.debug('The following exception has occurred: ' + e.getMessage());
            }
}
}

 

 

Puja_mfsiPuja_mfsi

Hi,

you have done a small mistake here.

please check your variable null check as :

 

if(Taskt==null || Taskt.length() == 0 ) {

    system.debug('Error validation');
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Subject is required'));
     return null;

}

 

 

Please let me know if u have any problem on same and if this post helps u please give KUDOS by click on star at left.

This was selected as the best answer
dmanidmani

Thanks Puja. That worked. Great.