You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Weng Kong Lee <ev...@gmail.com> on 2005/09/11 07:38:58 UTC
Problem with Validation of Struts DynaValidatorForm
Hi all,
I've just started exploring the use of Struts DynaValidatorForms. I've
tried to set-up a very simple login form with a username and password
fields, where both are required. Here's the relevant details:
1. struts-config.xml
The application message resource bundle has been configured with the
required validation messages for the validation framework, and the
validator plugin is also declared.
I declared a form bean as follows:
<form-bean name="loginForm"
type="org.apache.struts.validator.DynaValidatorForm">form-bean
name="loginForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="password" type="java.lang.String" />
<form-property name="userName" type="java.lang.String" />
<form-property name="userName" type="java.lang.String" />
</form-bean>
Then I declared a action tied to the form bean as follows:
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
type="project.struts.actions.LoginAction"
validate="true">
<forward name="success" path="/index.jsp" redirect="true"/>
<forward name="failure" path="/login.jsp" redirect="true"/>
</action>
2. login.jsp
This is the JSP with the form. It uses both client and server-side
validation.The code is as follows:
<body>
<html:errors/>
<html:form action="login" method="post" focus="userName"
onsubmit="return validateLoginForm(this);">
<table border="0">
<tr>
<td>Login:</td>
<td><html:text property="userName" /></td>
</tr>
<tr>
<td>Password:</td>
<td><html:password property="password" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit value="Login"/></td>
</tr>
</table>
</html:form>
<html:javascript formName="loginForm"/>
</body>
3. validation.xml
I declared both the "userName" and the "password" fields to be
"required", as follows:
<formset>
<form name="loginForm">
<field property="userName" depends="required">
<arg0 name="required" key="loginForm.userName"/>
</field>
<field property="password" depends="required">
<arg0 name="required" key="loginForm.password"/>
</field>
</form>
</formset>
4. Action class, LoginAction.java
Here's the execute mehod implementation. Basically, it rejects the
login when the userName and password are not both "test".
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaValidatorForm loginForm = (DynaValidatorForm) form;
String userName = loginForm.getString("userName");
String password = loginForm.getString("password");
if ((userName == null) || (!userName.equals("test"))) {
return mapping.findForward("failure");
} else if ((password == null) || (!password.equals("test"))) {
return mapping.findForward("failure");
} else {
return mapping.findForward("success");
}
}
I then tried to test the above by deploying the application, hitting
login.jsp and attempting to login with invalid userName and password.
The Javascript validation works fine, and the messages display
correctly => The message resource is configured correctly.
However when I turned off Javascript on my bowser to test the
server-side validation. The validation did not take place at all! If I
supplied a blank userName and/or password, it simply performs the
logic in LoginAction and sends me back to login.jsp, without any error
messages!
Anyone has any ideas what I might have missed out here?
Thanks in advance for any suggestions!
Weng Kong Lee
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Problem with Validation of Struts DynaValidatorForm
Posted by Weng Kong Lee <ev...@gmail.com>.
Hi Laurie,
Thanks for the suggestion!
After digging into the Tomcat console logs, I've finally found out
what the problem is. Seems like my copy of validation-rules.xml was
corrupted. The arguments for the validateRequired method were wrong.
Corrected that and now it works fine :-)
Thanks for the reminder though on the tpye-casting for the form. That
could save me a lot of brain-cells some time down the line...
Weng Kong Lee
On 9/12/05, Laurie Harper <la...@holoweb.net> wrote:
> You probably need your form to be declared as type
> org.apache.struts.action.DynaActionForm (rather than DynaValidatorForm).
> The difference between their behaviour is a little subtle so it's easy
> to get them mixed up. Try changing it and see if it works.
>
> L.
>
> Weng Kong Lee wrote:
> > Hi all,
> > I've just started exploring the use of Struts DynaValidatorForms. I've
> > tried to set-up a very simple login form with a username and password
> > fields, where both are required. Here's the relevant details:
> >
> > 1. struts-config.xml
> > The application message resource bundle has been configured with the
> > required validation messages for the validation framework, and the
> > validator plugin is also declared.
> > I declared a form bean as follows:
> > <form-bean name="loginForm"
> > type="org.apache.struts.validator.DynaValidatorForm">form-bean
> > name="loginForm" type="org.apache.struts.validator.DynaValidatorForm">
> > <form-property name="password" type="java.lang.String" />
> > <form-property name="userName" type="java.lang.String" />
> > <form-property name="userName" type="java.lang.String" />
> > </form-bean>
> > Then I declared a action tied to the form bean as follows:
> > <action
> > attribute="loginForm"
> > input="/login.jsp"
> > name="loginForm"
> > path="/login"
> > scope="request"
> > type="project.struts.actions.LoginAction"
> > validate="true">
> > <forward name="success" path="/index.jsp" redirect="true"/>
> > <forward name="failure" path="/login.jsp" redirect="true"/>
> > </action>
> >
> > 2. login.jsp
> > This is the JSP with the form. It uses both client and server-side
> > validation.The code is as follows:
> > <body>
> > <html:errors/>
> > <html:form action="login" method="post" focus="userName"
> > onsubmit="return validateLoginForm(this);">
> > <table border="0">
> > <tr>
> > <td>Login:</td>
> > <td><html:text property="userName" /></td>
> > </tr>
> > <tr>
> > <td>Password:</td>
> > <td><html:password property="password" /></td>
> > </tr>
> > <tr>
> > <td colspan="2" align="center"><html:submit value="Login"/></td>
> > </tr>
> > </table>
> > </html:form>
> > <html:javascript formName="loginForm"/>
> > </body>
> >
> > 3. validation.xml
> > I declared both the "userName" and the "password" fields to be
> > "required", as follows:
> > <formset>
> > <form name="loginForm">
> > <field property="userName" depends="required">
> > <arg0 name="required" key="loginForm.userName"/>
> > </field>
> > <field property="password" depends="required">
> > <arg0 name="required" key="loginForm.password"/>
> > </field>
> > </form>
> > </formset>
> >
> > 4. Action class, LoginAction.java
> > Here's the execute mehod implementation. Basically, it rejects the
> > login when the userName and password are not both "test".
> > public ActionForward execute(ActionMapping mapping, ActionForm form,
> > HttpServletRequest request, HttpServletResponse response) {
> > DynaValidatorForm loginForm = (DynaValidatorForm) form;
> > String userName = loginForm.getString("userName");
> > String password = loginForm.getString("password");
> > if ((userName == null) || (!userName.equals("test"))) {
> > return mapping.findForward("failure");
> > } else if ((password == null) || (!password.equals("test"))) {
> > return mapping.findForward("failure");
> > } else {
> > return mapping.findForward("success");
> > }
> > }
> >
> > I then tried to test the above by deploying the application, hitting
> > login.jsp and attempting to login with invalid userName and password.
> > The Javascript validation works fine, and the messages display
> > correctly => The message resource is configured correctly.
> >
> > However when I turned off Javascript on my bowser to test the
> > server-side validation. The validation did not take place at all! If I
> > supplied a blank userName and/or password, it simply performs the
> > logic in LoginAction and sends me back to login.jsp, without any error
> > messages!
> >
> > Anyone has any ideas what I might have missed out here?
> >
> > Thanks in advance for any suggestions!
> >
> > Weng Kong Lee
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Problem with Validation of Struts DynaValidatorForm
Posted by Laurie Harper <la...@holoweb.net>.
You probably need your form to be declared as type
org.apache.struts.action.DynaActionForm (rather than DynaValidatorForm).
The difference between their behaviour is a little subtle so it's easy
to get them mixed up. Try changing it and see if it works.
L.
Weng Kong Lee wrote:
> Hi all,
> I've just started exploring the use of Struts DynaValidatorForms. I've
> tried to set-up a very simple login form with a username and password
> fields, where both are required. Here's the relevant details:
>
> 1. struts-config.xml
> The application message resource bundle has been configured with the
> required validation messages for the validation framework, and the
> validator plugin is also declared.
> I declared a form bean as follows:
> <form-bean name="loginForm"
> type="org.apache.struts.validator.DynaValidatorForm">form-bean
> name="loginForm" type="org.apache.struts.validator.DynaValidatorForm">
> <form-property name="password" type="java.lang.String" />
> <form-property name="userName" type="java.lang.String" />
> <form-property name="userName" type="java.lang.String" />
> </form-bean>
> Then I declared a action tied to the form bean as follows:
> <action
> attribute="loginForm"
> input="/login.jsp"
> name="loginForm"
> path="/login"
> scope="request"
> type="project.struts.actions.LoginAction"
> validate="true">
> <forward name="success" path="/index.jsp" redirect="true"/>
> <forward name="failure" path="/login.jsp" redirect="true"/>
> </action>
>
> 2. login.jsp
> This is the JSP with the form. It uses both client and server-side
> validation.The code is as follows:
> <body>
> <html:errors/>
> <html:form action="login" method="post" focus="userName"
> onsubmit="return validateLoginForm(this);">
> <table border="0">
> <tr>
> <td>Login:</td>
> <td><html:text property="userName" /></td>
> </tr>
> <tr>
> <td>Password:</td>
> <td><html:password property="password" /></td>
> </tr>
> <tr>
> <td colspan="2" align="center"><html:submit value="Login"/></td>
> </tr>
> </table>
> </html:form>
> <html:javascript formName="loginForm"/>
> </body>
>
> 3. validation.xml
> I declared both the "userName" and the "password" fields to be
> "required", as follows:
> <formset>
> <form name="loginForm">
> <field property="userName" depends="required">
> <arg0 name="required" key="loginForm.userName"/>
> </field>
> <field property="password" depends="required">
> <arg0 name="required" key="loginForm.password"/>
> </field>
> </form>
> </formset>
>
> 4. Action class, LoginAction.java
> Here's the execute mehod implementation. Basically, it rejects the
> login when the userName and password are not both "test".
> public ActionForward execute(ActionMapping mapping, ActionForm form,
> HttpServletRequest request, HttpServletResponse response) {
> DynaValidatorForm loginForm = (DynaValidatorForm) form;
> String userName = loginForm.getString("userName");
> String password = loginForm.getString("password");
> if ((userName == null) || (!userName.equals("test"))) {
> return mapping.findForward("failure");
> } else if ((password == null) || (!password.equals("test"))) {
> return mapping.findForward("failure");
> } else {
> return mapping.findForward("success");
> }
> }
>
> I then tried to test the above by deploying the application, hitting
> login.jsp and attempting to login with invalid userName and password.
> The Javascript validation works fine, and the messages display
> correctly => The message resource is configured correctly.
>
> However when I turned off Javascript on my bowser to test the
> server-side validation. The validation did not take place at all! If I
> supplied a blank userName and/or password, it simply performs the
> logic in LoginAction and sends me back to login.jsp, without any error
> messages!
>
> Anyone has any ideas what I might have missed out here?
>
> Thanks in advance for any suggestions!
>
> Weng Kong Lee
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org