File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Custom Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Custom Validation" Watch "Custom Validation" New topic
Author

Custom Validation

Garandi Garandi
Ranch Hand

Joined: Jan 07, 2003
Posts: 192
I need to do some custom validation on my struts application. But when I run the jsp it does not validate.
Moreover when I do view source, I don't see the name of method in the file either.
I have the following code.
I made a jar file with this class in it and the jar file is in tomcat lib dir.


validation.xml I have the following lines.
<formset>

<form name="contactInfo">
<field property="nameFirst"
depends="contactInfoValidation">
<arg0 key="prompt.username"/>
</field>
</form>
</formset>

validator-rules.xml I have the following.

<validator name="contactInfoValidation"
classname="com.mypackage.SupportPortalValidation"
method="validateContactInfoField"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>
[ November 23, 2004: Message edited by: Garandi Garandi ]
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Moreover when I do view source, I don't see the name of method in the file either.


Why should you? You did not write the javascript version in your validator-rules.xml.

Are you trying to validate server-side or with javascript?


A good workman is known by his tools.
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
Hi
------------------------------------------------------------------------
public static boolean validateContactInfoField(Object bean, ValidatorAction action, Field field,
ActionMessage errors,Validator validator ,HttpServletRequest request){

--------------------------------------------------------------------------
validator-rules.xml I have the following.

<validator name="contactInfoValidation"
classname="com.mypackage.SupportPortalValidation"
method="validateContactInfoField"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>

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


There is difference in method params for ActionMessages that you have mentioned in your validator-rules.xml, whereas its mentioned as ActionMessage in your validation class file. thats why the method is not being calles because it differ in its signature- in class and in validator-rule.xml file.

Garandi Garandi
Ranch Hand

Joined: Jan 07, 2003
Posts: 192
Thank you Marc and Damanjit.
I am planning to do server side as well as client side validation. But as far as my server side validation goes for now, I can see SOME of the log msgs printed. but page is forwarded as there is no error on the page.

validation.xml files

<formset>

<form name="contactInfo">
<field property="nameFirst"
depends="jawadcontactInfoValidation">
<arg0 key="prompt.username"/>
</field>
</form>
</formset>

validator-rules.xml

<validator name="contactInfoValidation"
classname="com.mypackage.validation.SupportPortalValidation"
method="validateContactInfoFieldJawad"
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"/>

and the java class for validation:


lines after :


does not print.
Q1: How to make the page to display with error, without forwarding it to success page?

Thank you in advance
Garandi
[ November 24, 2004: Message edited by: Garandi Garandi ]
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346


<form name="contactInfo">
<field property="nameFirst"
depends="jawadcontactInfoValidation">
<arg0 key="prompt.username"/>
</field>
</form>
</formset>

validator-rules.xml

<validator name="contactInfoValidation"
classname="com.mypackage.validation.SupportPortalValidation"
method="validateContactInfoFieldJawad"
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"/>


In your validation.xml depends="jawadcontactInfoValidation"> doesn't match with validator name="contactInfoValidation" in validator-rules.xml file.
and so validation is not being performed and also its better if you also check the method name in validator-rules.xml file with method name in your validation class file.

Hope this will fix the problem.

Garandi Garandi
Ranch Hand

Joined: Jan 07, 2003
Posts: 192
Thank you Damanjit for your reply; server validation is working now, but I have problem with client validation.

This is valitator-rules.xml file

<validator name="contactInfoValidation"
classname="com.mycom.sp.validation.SupportValidation"
method="validateContactInfoField"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
msg="errors.required">
<javascript><![CDATA[
function myValidation(form) {
alert("I am from custom validation");
return false;
}
]]>
</javascript>
</validator>

Q1: Why alert does not pop up on the screen?
Q2: Should I use jsFunction if yes, how?
Thank you in advance
Garandi
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
Hi,

You can specify javascript function or jsFunction both in validator-rule.xml file.

The code in your application also looks fine.

Have you specified the tag :

<html:javascript formName="logonForm"/>
in your jsp file to enable the client side validation.

For more reference you can have a look at this web site:
http://www.oracle.com/technology/oramag/oracle/04-jan/o14dev_struts.html

and for creating jsfunction: you have to create a xxx.js( with javascript functions) file in WEB-INF directory. and configure struts-config.xml like :

<plug-in className="net.sf.struts.flow.FlowPlugIn">
<set-property property="scripts" value="/WEB-INF/xxx.js" />
</plug-in>

Here is a link from where I found this information(Although I have never used jsfunction till now.) :
http://struts.sourceforge.net/struts-flow/wizard-example.html

This link shows the contents of org.apache.commons.validator.javascript package which has all the definitions for jsfunctions that are used in validator-rules.xml file.
http://issues.apache.org/bugzilla/attachment.cgi?id=10185
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
Hi,

In my last reply I mentioned using

<plug-in className="net.sf.struts.flow.FlowPlugIn">
<set-property property="scripts" value="/WEB-INF/xxx.js" />
</plug-in>

But the class specified in className doesn't come with jakarta-struts, I don't know where to find this file(I mean I searched the internet and couldn't find it.)

Although I have never come across any example or struts documentation which specify the possibility of mentioning java script file also in plug-in tag
like : but you can try using it:

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validations.xml,/WEB-INF/xxx.js"/>
</plug-in>

This way the ValidatorPlugIn class will know of js file too.

I am not sure if it works or not this way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Custom Validation
 
Similar Threads
Automatic Validation does not work
Cannot load a validator resource
LookupDispatch action with validator
Having problem with struts validator for error message
Unable to validate minlength