aspose file tools*
The moose likes Struts and the fly likes Form is not being validated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Form is not being validated" Watch "Form is not being validated" New topic
Author

Form is not being validated

as chhipa
Greenhorn

Joined: Sep 04, 2006
Posts: 26
Hi all,
I'm new to struts and wants to validate my form field in input.jsp for the minlength. If the length is less than 5 then the control must return back to the input.jsp other wise it must go to success.jsp.
But at present what happening that if user enters less than 5 character than also the success.jsp page is being displayed instead of input.jsp.


Follwing is the struts-config file

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config
PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<global-forwards>
<forward name="home" path="/home.do" />
</global-forwards>
<form-beans>
<form-bean name="lookupForm" type="ch03.LookupForm" />
<form-bean name="inputForm" type="ch03.InputForm" />
</form-beans>

<action-mappings>
<action path="/lookup" type="ch03.LookupAction" name="lookupForm" >
<forward name="success" path="/quote.jsp" />
<forward name="failure" path="/index.jsp" />
</action>
<action path="/inputSubmit" type="ch03.InputAction" name="inputForm" scope="request" validate="false" input="/input.jsp" >
<forward name="success" path="/success.jsp" />
</action>
<action path="/home" forward="/quote.jsp" />

</action-mappings>
<message-resources parameter="ApplicationResources" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml" />
</plug-in>

</struts-config>
--------------------------------------------------------------------

Follwing is the input.jsp page

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<body>
<logic:messagesPresent>
There were error
<ul>
<font color='red'>
<html:messages id="error" >
<li><%=error %></li>
</html:messages>
</font>
</ul>
</logic:messagesPresent>
<html:form action="inputSubmit">
<bean:message key="inputForm.userName" />
<html:text property="userName" /> <br/>
<html:submit value="ok" />
</html:form>
</body>
</html>

---------------------------------------------------------

Follwing is InputForm.java

package ch03;

import org.apache.struts.action.ActionForm;
import org.apache.struts.validator.ValidatorForm;
public class InputForm extends ValidatorForm
{
public String userName;
public String getUserName()
{
return userName;
}
public void setUserName(String string)
{
userName=string;
}
}
----------------------------------------------------
Following is InputAction.java

package ch03;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InputAction extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception
{
InputForm inputForm=(InputForm)form;
System.out.println(inputForm.getUserName());
return mapping.findForward("success");
}
}
--------------------------------------------------------------------

and follwing is snippet from validation.xml file

<form name="inputForm">
<field property="userName"
depends="minlength">
<arg0 key="inputForm.userName" />
<arg1 key="$(var:minlength)"
name="minlength"
resource="false" />
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
</field>
</form>
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10290
    
168

<action path="/inputSubmit" type="ch03.InputAction" name="inputForm" scope="request" validate="false" input="/input.jsp" >


Change it to:



[My Blog] [JavaRanch Journal]
as chhipa
Greenhorn

Joined: Sep 04, 2006
Posts: 26
Thank you for your reply but setting

validate="true"

is not making any difference.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
The suggestion by Jaikiran is needed (set validate="true"). Other than that, it looks pretty good. Do you have validation working for other pages? I would suggest declaring userName as private in your form, but I don't see where that would make a difference.

- Brent
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10290
    
168

Honestly, i havent used Struts a lot. But, shouldnt you be overriding the validate method in your InputForm? Something like:

as chhipa
Greenhorn

Joined: Sep 04, 2006
Posts: 26
Hi all,
I have found the solution.Actually I'm using struts 1.3.5. and in this case culprit is validation.xml file.

Previously my validation.xml file was as follwing which is used in the older version

<form name="inputForm">

<field property="userName" depends="minlength">
<arg0 key="inputForm.userName" resource="true" />
<arg1 name="minlength" key="${var:minlength}" resource="false" />
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
</field>

</form>


but in struts 1.3.5 it will be like this
(Pay attention to arg element and position attribute )

<form name="inputForm">

<field property="userName" depends="minlength">
<arg key="inputForm.userName" resource="true" position="0"/>
<arg name="minlength" key="${var:minlength}" resource="false"
position="1"/>
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
</field>

</form>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Form is not being validated