If I recall, Niall has done a bunch of work in this area since that time. Sorry but I haven't kept up with it very well. I think you should wait for a post from Niall or Joe on this matter for a definitive answer then. But this works. This subject comes up a lot, and I also am interested in a nice solution. Right now I have a lot of JSPs with logical switches for every field that include the line of code below or something similar.
Hope this helps, Erik
Rodolfo García Esteban/CYII wrote:
Hi,
I´m using struts 1.2.4, after validate a form. I need to know if there is a message for a property before print, how can I do?
Thanks in advance,
Rodolfo
At 3:49 PM -0600 11/8/04, Struts User wrote:
Hello,
Currently, I am using struts validator to validate the fields in my ActionForm.
Before I updated to struts 1.2.4, I could add an error this way - errors.add(
"username", new ActionError("error.username.required"));
If the validation failed, the error message for username will be displayed right beside the user name text field if I use <P>Username: <html:text property="username"/></P>.
Can someone tell me how to display error message beside an input field using struts validator?
I've never seen any automatic message placement. You can access messages like this:
<html:messages property="username" id="msg"></html:messages>
html:messages is effectively a combination logic/iterator tag. If there are any "username" messages in the ActionMessages object saved as the "errors" messages, the body of html:messages will be evaluated once for each, with a scripting variable of type String defined with the name specified in the "id" attribute. You can use c:out or bean:write to display this value, wrapped with span, div, or other tags which format your messages correctly.
I kind of think someone talked on the list once about making something which rendered an HTML "label" tag and which was also "smart" about the presence of errors. I like the idea of something like that in general, but wonder if you'd be able to specify something suitably general for inclusion in Struts. Seems like it might be better left for local development.
Joe
. . .
At 5:10 PM -0500 11/8/04, Erik Weber wrote:
Here is a way to do it that works with 1.1:
<logic:messagesPresent name="org.apache.struts.action.ERROR" property="username">
<bean:message key="error.username.required"/>
</logic:messagesPresent>
<p>Username: <html:text property="username"/></p>
Omitting the "name" attribute in this would have the same effect as including it - it's the default. It's suggested that if you are storing messages in your actions using "saveErrors" or "saveMessages" (or dealing with validation errors, which are stored as if using "saveErrors") then you shouldn't specify "name" literally; instead, you can either rely on the default (to get errors) or use the "message" attribute with a value of "true" (to get saveMessages messages) or "false" (to get errors -- that is, the default behavior). This helps to "encapsulate" the logic of how Struts handles those messages, and makes your JSP a bit less verbose also.
Additionally, your code above assumes that the nature of the error message is such that you know that the user should be shown the "error.username.required" message -- it's probably more flexible to let the validation/error handling framework create an ActionMessage object with that key itself, and then you would use the messages object; this way if any other error should come up (say you add a max-length restriction, or forbidden characters), then you don't have to change your JSP.
In another response I demonstrated how html:messages can be used to achieve the same result you had above, plus this added flexibility I mention.
Some people still like to use "html:errors", which predates html:messages and which has a slightly different syntax for providing any HTML which might "wrap" your messages. Personally, I prefer html:messages.
None of this changed from Struts 1.1 to Struts 1.2.
Hope this helps, Joe
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]