• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Custom Validation

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



<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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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've got no option but to sell you all for scientific experiments. Or a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic