Hi Elco,
I been fooling around with struts for a couple of days mow...
Let me have a shot,
I have the following ActionForm class that does the validation
-----
ActionErrors errors = new ActionErrors();
if ((name == null) || (name.length() < 1))
errors.add("name", new
ActionError("error.name.required"));
if ((age == null) || (age.length() < 1))
errors.add("age", new
ActionError("error.age.required"));
return errors;
-----
In my jsp i have the following:
-----
<html:form action="/register.do" focus="name">
<html:text property="name" size="16" maxlength="16"/>
<html:errors property="name"/>
<html:text property="age" size="2" maxlength="2"/><html:errors
property="age"/>
<html:submit property="submit" value="Submit"/>
-----
When i submit a form without either name or age the validate method will
populate the errors object with String described in my
ApplicationResource.properties file
Struts will then return me to the page, prefill the form with any valid data
and print the error message that we added to the erros object in the
validate method...
This works, if you have something similar it should also work...
regards
Mikkel Bruun
-----Original Message-----
From: Eelco van Kuik
To: Struts-User (E-mail)
Sent: 15-05-2001 10:54
Subject: newbie: Problem with ActionErrors
Dear colleague developers,
We are struggling with a problem to get the struts-forms working. It
might
be a newbie problem, but it is still difficult to find out which newbie
problem we made. ;-) I included the most important struts problems at
the
bottom.
No FAQ/examples/etc. could solve our problem, so I think that it might
have
something to do with a more complicated issue.
We want:
Serverside form validation:
1. When an error is added to the ActionErrors we want to display it at
the
top (iterate over the list)
2. Show the same form again, and
3. WITH the input (even though it might not be right) shown in the input
fields.
We have:
1. Whenever we raise an error we get the message at the top of the page:
Validation Error
You must correct the following error(s) before proceeding:
And no list of the errors.......
2. We get the same form allright, but with NO VALUES whatsoever.
Guys, WE ARE STUCK. :-(
So any help would be greatly apreciated.
Regards,
Eelco & Angela
************************************************************************
****
***********************************************************
here the (stripped) jsp code:
<form:errors/>
<form:form name="sparenform"
type="nl.rabobank.www.rekenmodellen.sparen.SparenActionForm"
action="/rekenmodellen/sparenresult" focus="invoerGetal1">
<form:text property="invoerGetal1" size="10" maxlength="10"/>
</form:form>
here the (stripped) actionclass code:
ActionErrors errorsHTML = new ActionErrors();
errorsHTML = sparenActionForm.checkForm();
if (!errorsHTML.empty())
{
cat.debug("!errorsHTML.empty()");
saveErrors(request, errorsHTML); //request.setAttribute
("sparen.error", errorsHTML);
// return to the original form
return (new ActionForward(mapping.getInput()));
}
else
{
..no errors
}
here the form validator function(in the ActionForm):
public ActionErrors checkForm()
{
ActionErrors errors = new ActionErrors();
/**
* error inleg: getal heeft verkeerde waarde
*/
if (!berekeningtype.equalsIgnoreCase("i") && (inleg <= 0 ||
inleg >=
1000000))
{
cat.debug("fout gemaakt in inleg");
errors.add("invoerGetal1",new ActionError("inleg moet
tussen
de 0 en 1.000.000 zijn !"));
}