You need to sign in to do that
Don't have an account?

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);
}
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:
Controller code:
All Answers
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.
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...
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:
Then in the page you'd have something like:
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:
Controller code:
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
That sounds like you are missing the boolean 'error' controller property that determines whether the style gets applied.
hi bob,
I am not able to get your point.
Can you make it clear?
Thanks,
S.Sivakumar.
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.
Is that
public boolean error{get;set;}
must be declared in controller?
If so where it is assigned to true?
Thanks,
S.Sivakumar
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.
You can use following way in your controller to display error :
objectName.field.adderror('Error Message');
Regards,
Ajay