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
JeeedeeeJeeedeee 

How to add error message to specific inputText / inputfield

I am having trouble finding a way to populate error messages to my visual force page. I would like to add a (custom) error message to a inputText on my visual force page like standard behaviour(red lines, and red error message). What I can accomplish is adding it to the top of the page, but how should I add it to one specific field(inputText or inputField)?

 

VF:code:

<apex:page>
<apex:form>
<apex:messages /> <!-- This is showing the errormessage at top -->
<apex:pageBlock mode="detail">
<apex:pageblockSection id="search" columns="1" rendered="{!editModus}">
<apex:outputLabel value="Value"></apex:outputLabel>
<apex:inputText value="{!aValue}"></apex:inputText> <-- HERE I WOULD LIKE TO have the error messages
<apex:commandButton value="Validate" action="{!validate}"/>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 And (a part) of my controller, now it adds the errormessage, but it is shown on top of the page

 

    public void validate() {
// other logic. I add a message to the page, but how can I add it to the input text
ApexPages.Message message = new ApexPages.message(ApexPages.severity.ERROR,'An error message.');
ApexPages.addMessage(message);
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JeeedeeeJeeedeee

Bob thanks a lot, that indeed did the trick. Sometimes life is simple :) I added one thing to make it look even better... I added a dynamic styleClass variable, to make the inputText box become red when there is an error.

Here's the final code! Maybe it's helpfull for others
VF page:

 

<apex:outputLabel value="Postcode"></apex:outputLabel>
<apex:inputText styleClass="{!classInputPostalCode}" value="{!postalCode}"/>
<apex:outputText value="{!errorMessage}" styleClass="errorMsg" rendered="{!LEN(errorMessage)>0}"/>

 Controller code:

 

public String classInputPostalCode {get;set;}
public String postalCode {get;set;}
public String errorMessage {get;set;}

public void validate() {
if (error) {
classInputPostalCode = 'error'; // put the errorclass, red borders
errorMessage = 'Postal code not found';
} else {
classInputPostalCode = '';
errorMessage = '';
}
}

 

 

All Answers

bob_buzzardbob_buzzard

The apex:message component allows you to do this - you'll need to propagate your error message to the page via a property rather than by adding an error.

 

The VisualForce Developer's Guide has an example of doing this on page 240 of the Summer '10 version.

JeeedeeeJeeedeee

Hmmz, so I have to add something like this, but how can I add the error's in my controller class? I am still a little bit confused, expecially since I would like to use inputText...

 

 

<apex:inputText id="inputPostalCode" value="{!postalCode}"></apex:inputText>
<apex:message for="inputPostalCode"></apex:message>

 

 

bob_buzzardbob_buzzard

 

Sorry, I think I misunderstood your question!

 

I think if you want to put errors around regular input components you'll have to manage it yourself, as the "standard" validation stuff works on sobjects and fields.

 

Thus you'd need to set an error value in your controller that can be retrieved from the page:

 

 

public String inputTextErrorMessage {get; set;}

public void validate() 
{
    if (error)
    {
       inputTextErrorMessage='An error occurred';
    }
}

 

 

Then in the page you'd have something like:

 

 

<apex:inputText value="{!aValue}"></apex:inputText>
<apex:outputText value="{!inputTextErrorMessage}" rendered="{!LEN(inputTextErrorMessage)>0)}"/>
<apex:commandButton value="Validate" action="{!validate}"/>

 

 

 

 

 

 

 

JeeedeeeJeeedeee

Bob thanks a lot, that indeed did the trick. Sometimes life is simple :) I added one thing to make it look even better... I added a dynamic styleClass variable, to make the inputText box become red when there is an error.

Here's the final code! Maybe it's helpfull for others
VF page:

 

<apex:outputLabel value="Postcode"></apex:outputLabel>
<apex:inputText styleClass="{!classInputPostalCode}" value="{!postalCode}"/>
<apex:outputText value="{!errorMessage}" styleClass="errorMsg" rendered="{!LEN(errorMessage)>0}"/>

 Controller code:

 

public String classInputPostalCode {get;set;}
public String postalCode {get;set;}
public String errorMessage {get;set;}

public void validate() {
if (error) {
classInputPostalCode = 'error'; // put the errorclass, red borders
errorMessage = 'Postal code not found';
} else {
classInputPostalCode = '';
errorMessage = '';
}
}

 

 

This was selected as the best answer
Sivakumari2iSivakumari2i

What does error variable mean here.

If i use this code, I am getting the error as "error variable not defined".

 

Please correct me.

 

Thanks,

S.Sivakumar

bob_buzzardbob_buzzard

That sounds like you are missing the boolean 'error' controller property that determines whether the style gets applied.

Sivakumari2iSivakumari2i

hi bob,

 

I am not able to get your point.

 

Can you make it clear?

 

Thanks,

S.Sivakumar.

bob_buzzardbob_buzzard

Yes - the if statement refers to a controller property named 'error', which is of type boolean.  This is set when an error occurs, and causes the controller to produce an error message.  I think you are missing this controller property.

Sivakumari2iSivakumari2i

Is that

 

public boolean error{get;set;}

 

must be declared in controller?

 

If so where it is assigned to true?

 

Thanks,

S.Sivakumar

bob_buzzardbob_buzzard

Yes, that is how you declare it.

 

It gets set to true when your controller detects a problem - you'll need to write that business logic.

Ajay_SFDCAjay_SFDC
Hi there ,
 You can use following way in your controller to display error :
 
objectName.field.adderror('Error Message');

Regards,
Ajay