Here are a couple of old posts I saved that might help too:



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



Not sure if that tag has changed for 1.2.

Erik


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?

Thanks,
Lee

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




The "other response" Joe mentioned I think was this one:

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
------------------------

But I think Niall recently mentioned some improvements in this area in 1.2.6 . . .

Erik




Adam Jenkins wrote:

Thanks for that, I'll give it a go.  Is there anyway you can accomplish
it without having to put html in the resource bundle?

On Sun, 2005-02-27 at 19:04 -0500, Erik Weber wrote:


Last I checked, Struts came configured to do what I think you are asking by default.

This is an example of the (Struts 1.1) ApplicationResources.properties file:

#header and footer for form validation error messages
errors.header=<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr align="left"><td><strong><font color="red">input error</font></strong></td></tr><tr><td>&nbsp;</td></tr><tr align="left"><td>Please resubmit the form after correcting your input to meet the following requirement(s):<ul>
errors.footer=</ul></td></tr><tr><td>&nbsp;</td></tr></table>



#form validation error messages #(rendered between header and footer above) errors.required=<li>{0} is required.</li> errors.minlength=<li>{0} cannot contain fewer than {1} characters.</li> errors.maxlength=<li>{0} cannot contain more than {2} characters.</li> errors.invalid=<li>{0} is invalid.</li> errors.byte=<li>{0} must be an byte.</li> errors.short=<li>{0} must be an short.</li> errors.integer=<li>{0} must be an integer.</li> errors.long=<li>{0} must be an long.</li> errors.float=<li>{0} must be an float.</li> errors.double=<li>{0} must be an double.</li> errors.date=<li>{0} is not a date.</li> errors.range=<li>{0} is not in the range {1} through {2}.</li> errors.creditcard=<li>{0} is not a valid credit card number.</li> errors.email=<li>{0} is invalid.</li> #user plugins errors.twofields=<li>{0} must have the same value as {1}.</li> errors.dateRange=<li>{0} is not an acceptable date.</li>


Then a simple <html:errors/> results in the bulleted list of all current validation errors, assuming your validation.xml is set up properly so that errors are matched with the messages in the properties file.


Hope this helps.

Erik



Adam Jenkins wrote:



Hi All,

Is there a simple way to iterate over all the validation errors.  I
don't want to just do <html:errors..../>  I want to actually for a
<ul><li>...</li></ul> of all the errors that occured when submitting the
page.

Is this possible?  Does anyone have an example I could use?

Cheers
Adam


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]






---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to