Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LookupDispatch action with validator

 
Vicky Pandya
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 354
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vicky Pandya
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 354
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tried with the formName also as you said but no luck. I have posted seperate thread with the same issue with very simple code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic