The solution given below does not work for the case I described.  jsType.jsp
defines one form with several masked field validations.

The example I'm having difficulty with is multiple forms with multiple
fields with masked field validation which causes the Struts Validator to
generate multiple versions of the function

        function mask () {}

for each of the <validator:javascript> entries shown below:

                (1) <validator:javascript formName="form1"
dynamicJavascript="true" staticJavascript="false"/>

                (2) <validator:javascript formName="form2"
dynamicJavascript="true" staticJavascript="false"/>

                
The first validator tag generates the following code (note the function
mask() )

                <SCRIPT LANGUAGE="Javascript1.1"> 
                <!-- Begin 

                        var bCancel = false; 

                        function validateForm1(form) {

                        if (bCancel) 
                                return true; 
                        else 
                                return validateMask(form); 
                        } 

                        function mask () { 
                         this.aa = new Array("field6", "Field 6 is
invalid.", new Function ("varName", "this.mask=/^\\d{7}/;  return
this[varName];"));
                        } 


                //  End -->
                </SCRIPT>
                
while the second validator tag generates the following code (note the
additional function mask() : NAMING CONFLICT )

                <SCRIPT LANGUAGE="Javascript1.1"> 
                <!-- Begin 

                        var bCancel = false; 

                        function validateOmpModel(form) {

                        if (bCancel) 
                                return true; 
                        else 
                                return validateRequired(form) &&
validateMask(form) && validateMaxLength(form) && validateMinLength(form) &&
validateInteger(form) && validateRange(form); 
                        }

                        function mask () { 
                        this.aa = new Array("field1", "Field 1 is invalid.",
new Function ("varName", "this.mask=/^\\d{6}/;  return this[varName];"));
                        this.ab = new Array("field2", "Field 2 is invalid.",
new Function ("varName", "this.mask=/^[ALST]/; this.maxlength='1';  return
this[varName];"));
                         this.ac = new Array("field3", "Field 3 is
invalid.", new Function ("varName", "this.mask=/^\\d{6}/;
this.maxlength='6';  return this[varName];"));
                        this.ad = new Array("field4", "Field 4 is invalid.",
new Function ("varName", "this.mask=/^\\d{1}/;  return this[varName];"));
                         this.ae = new Array("field5", "Field 5 is
invalid.", new Function ("varName", "this.mask=/^\\c{1}/;  return
this[varName];"));
                        } 

                //  End -->
                </SCRIPT>


In the static JavaScript generated by the following tag

                <script language="JavaScript1.1"
src="../includes/staticStrutsValidatorJavascript.jsp"></script>

                
                staticStrutsValidatorJavascript.jsp
                ------------------------------------
                <%@ page language="java" %>
                <%-- set document type to Javascript (addresses a bug in
Netscape according to a web resource --%>
                <%@ page contentType="application/x-javascript" %>

                <%@ taglib uri="/WEB-INF/struts-validator.tld"
prefix="validator" %>

                <validator:javascript dynamicJavascript="false"
staticJavascript="true"/>
                -------------------------------------

the problem lies in the last statement shown in the code listing below which
is defined in validator-rules.xml and generated by the static JavaScript

      <validator name="mask"
 
classname="org.apache.struts.validator.util.StrutsValidator"
                 method="validateMask"
                 depends="required"
                 msg="errors.invalid">
         <javascript><![CDATA[
            function validateMask(form) {

                var bValid = true;
                var focusField = null;
                var i = 0;

                var fields = new Array();

                oMasked = new mask(); 

                        ....

When the function runs the line oMasked = new mask();, it always retrieves
the second mask() and thereby errs when attempting to validate the first
form since (in the example I gave above), there is no field by the name of
field6 in the second function mask() instance ( I get a JavaScript error on
the following line form[oMasked[x][0]].value since form[oMasked[x][0]] is
undefined ).

I hope I have been clearer about the problem now.

The solution I had in mind was to modify the Struts Validator to generate
custom validation functions.  So, for the example I gave above, it would
generate the following functions:

        function maskForm1()
        {
                ....
        }

        function maskForm2()
        {
                ....
        }

and the validateMask() function in validator-rules.xml would need to be
modified to generate the following:

      <validator name="mask"
 
classname="org.apache.struts.validator.util.StrutsValidator"
                 method="validateMask"
                 depends="required"
                 msg="errors.invalid">
         <javascript><![CDATA[
            function validateMask(form) {

                var bValid = true;
                var focusField = null;
                var i = 0;

                var fields = new Array();

                
                        functionName = 'oMasked = new mask' + form.name +
'()';
                        eval(functionName); 

                ....


Is there any other solution without my having to modify validator code to
generate the functions as shown above?  

Michael

-----Original Message-----
From: David Winterfeldt [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, June 12, 2002 5:55 PM
To: Struts Users Mailing List
Subject: Re: Struts Validator 1.1


Creating a Separate Page for Static JavaScript
If you want to just generate the dynamic JavaScript on
you form page and have a separate page with the static
JavaScript to take advantage of browser caching you
can use the dynamicJavascript and staticJavascript
attributes to the JavascriptValidatorTag. You can turn
each attribute's generation of JavaScript on and off
by putting in true or false (they default to true).
Reference jsType.jsp and staticJavascript.jsp in the
main example webapp for a working example.

In your form page:
    <validator:javascript formName="typeForm"
dynamicJavascript="true" staticJavascript="false"/>
    <script language="Javascript1.1"
src="staticJavascript.jsp"></script>

    This wouldn't work right in IE 5.0 when I made the
source an attribute of the tag.

In your static JavaScript page.
    <%@ page contentType="application/x-javascript" %>
    <validator:javascript dynamicJavascript="false"
staticJavascript="true"/>

http://home.earthlink.net/~dwinterfeldt/jsptags.html

There is an example of this in the example webapp.

David

--- [EMAIL PROTECTED] wrote:
> 
> Does the Struts Validator  v1.0 or v1.1 allow
> multiple page form
> validations?
> 
> I have a page with two forms, each requiring
> validation before submission.
> 
>       Form 1: <html:form action="form1Action.do"
> method="post"
> onsubmit="return validateForm1(this)">
>                               
>       Form 2: <html:form action="form2Action.do"
> method="post"
> onsubmit="return validateForm2(this)" >
> 
> Both of these forms contain one field that requires
> a mask validation.
> 
> However, version 1.0 of the Struts Validator which I
> am using generates two
> JavaScript functions 
> 
>               <SCRIPT LANGUAGE="Javascript1.1"> 
>               <!-- Begin 
> 
>                       var bCancel = false; 
> 
>                       function validateForm1(form) {
> 
>                       if (bCancel) 
>                               return true; 
>                       else 
>                               return validateMask(form); 
>                       } 
> 
>                       function mask () { 
>                       this.aa = new Array("field1", " is invalid.",
> new
> Function ("varName", "this.mask=/^\\d{7}/;  return
> this[varName];"));
>                        } 
>               //  End -->
>               </SCRIPT>
> 
>               <SCRIPT LANGUAGE="Javascript1.1"> 
>               <!-- Begin 
> 
>                       var bCancel = false; 
> 
>                       function validateForm2(form) {
> 
>                        if (bCancel) 
>                               return true; 
>                       else 
>                               return validateMask(form); 
>                       } 
> 
>                        function mask () { 
>                        this.aa = new Array("field2", "Field 2 is
> invalid.", new Function ("varName",
> "this.mask=/^\\d{6}/;  return
> this[varName];"));
>                       this.ab = new Array("field3", "Field 3 is
> invalid.",
> new Function ("varName", "this.mask=/^[ALST]/;
> this.maxlength='1';  return
> this[varName];"));
>                       this.ac = new Array("field4", "Field 4 is
> invalid.",
> new Function ("varName", "this.mask=/^\\d{6}/;
> this.maxlength='6';  return
> this[varName];"));
>                       } 
>               //  End -->
>               </SCRIPT>
> 
> By defining two functions both with the same name (
> i.e. function mask ),
> the Struts Validator 1.0 prevents me from performing
> validation on multiple
> forms on the same page with the same validation
> rules (i.e. mask in this
> case).
> 
> Any suggestions or workarounds?  If v1.0 does not
> support this, does v1.1
> address this issue by generating unique function
> names per form?
> 
> Any help is greatly appreciated.
> 
> Thanks.
> 
> Michael
> 
> --
> To unsubscribe, e-mail:  
> <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
> <mailto:[EMAIL PROTECTED]>
> 


__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com

--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to