This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes LookupDispatch action with validator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "LookupDispatch action with validator" Watch "LookupDispatch action with validator" New topic
Author

LookupDispatch action with validator

Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
My all Action classes extend my base class, which in turn extends LookupDispatchAction. While tryig to configure validator I found its NOT working with my action class. Here is what I am doing in my app.

Actionclass extends BaseClass (which extends LookupDispatchAction){
}
ActionForm extends org.apache.struts.validator.ValidatorForm{
String property;
}

<action path="/searchAction" type="com.action.SearchAction"
name="searchForm"
scope="request"
validate="true" input="/jsp/logon.jsp" parameter="method">

part of validation.xml
<formset>
<form name="searchForm">
<field property="freeText" depends="required">
<arg key="searchForm.freeText.displayname"/>
</field>
</form>
</formset>
searchForm.freeText.displayname=Search in properties file.

part of validator-rules.xml
<validator name="required"
classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>

part of struts-config.xml
<!-- Validator Configuration -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/>
<set-property property="stopOnFirstError" value="false"/>
</plug-in>

Now issue is when I am trying to get validator work for searchForm above. Nothing seems to be working. Also after I resolve this I would need to find out how to use different validation rules for different methods of my action class (extends LookupDispatchAction). Any ideas?

Thanks,
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
you need to declare your form-bean type to org.apache.struts.validator.DynaValidatorForm in order for declarative validation to work (http://struts.apache.org/api/org/apache/struts/validator/DynaValidatorForm.html).

with XXXDispatchAction, conditional validation dependend on which method gets called is a b.yatch. you can use requiredif or validwhen.

or you need to define different mapping for each of your dispatch method that points to the same DispatchAction, then use the action mapping path attribute to define your validations.

or you can have different action mappings to the same DispatchAction, but different duplicate forms with different name; then use the form name to define your validation.

in all case, hairy, hairy...
http://struts.apache.org/userGuide/dev_validator.html.


-/a<br />certified slacker...yes, my last name is 'do' - <a href="http://www.luckycouple.com" target="_blank" rel="nofollow">luckycouple.com</a>
Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
alan,

I have following code but doesn't seem to work. no validation is taking place. where am I going wrong?

part of struts-config.xml

<form-bean name="searchForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="freeText" type="java.lang.String"/>
</form-bean>

<action path="/searchAction" type="com.action.SearchAction"
name="searchForm"
scope="request"
validate="true" input="/jsp/logon.jsp" parameter="method">
<forward name="search" path="risksearch.page"/>
<forward name="newrisk" path="/jsp/addrisk.jsp"/>
</action>
<!-- Validator Configuration -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/>
<set-property property="stopOnFirstError" value="true"/>
</plug-in>

following is my validation.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

<form-validation>
<formset>
<form name="/searchAction">
<field property="freeText" depends="required">
<arg key="searchForm.freeText.displayname"/>
</field>
</form>
</formset>
</form-validation>

here is my validator-rules.xml
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

<form-validation>
<global>
<validator name="required"
classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>
</global>
</form-validation>
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
in your validation.xml...
if your form-bean is of type DynaValidatorForm, you need to use the form name (see below). if you want to use the action mapping path as you have right now, you need to declare your form-bean as type DynaValidatorActionForm. pick your poison. i haven't used the action path to define the form validations, per the struts validation documentations, that seems to be the case.

<form-validation>
<formset>
<form name="searchForm">
<field property="freeText" depends="required">
<arg key="searchForm.freeText.displayname"/>
</field>
</form>
</formset>
</form-validation>
Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
tried with the formName also as you said but no luck. I have posted seperate thread with the same issue with very simple code.
 
wood burning stoves
 
subject: LookupDispatch action with validator
 
Similar Threads
Another "Validator Plugin" Problem
Struts validation error message not appearing
Form validation in struts
Need step by step procedure to use Validation framework
How to use Validators