File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error 'required' is undefined

 
alan wamser
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm try to configure struts validation and keeping getting a javascript error: Error 'required' is undefined.

1. Add the plugin to the struts-config.xml

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

2. Created the validation.xml file

<?xml version="1.0" encoding="windows-1252"?>
<form-validation>
<formset>
<form name="transferForm">
<field property="associateName" depends="required" >
<arg0 key="label.assoicateName" />
</field>
</form>
</formset>
</form-validation>

3. Added the following the the jsp file...

<html:form action="/saveTransfer.do" onsubmit="return validateTransferForm(this); ">
<TABLE border="1" width="50%">
<TR>
<TD class="formText"><bean:message key="label.assoicateName"/> </TD>
<TD>
<html:text property="associateName" />
</TD>
</TR>
and the onsubmit.

<html:form action="/saveTransfer.do" onsubmit="return validateTransferForm(this); ">

4. Here is the Action information

<action
input="/index.jsp"
name="transferForm"
path="/saveTransfer"
scope="request"
validate="true"
type="com.haysmed.jobtransfer.struts.action.SaveTransferAction">
<forward name="success" path="/index.jsp" />
</action>

5. I've extended the Actionform as the following...

public class TransferForm extends ValidatorActionForm {


So, what have I missed??? Why the javascript error???

[ February 10, 2006: Message edited by: alan wamser ]

[ February 10, 2006: Message edited by: alan wamser ]
[ February 10, 2006: Message edited by: alan wamser ]
 
alan wamser
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
function validateRequired(form) {
var isValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
error ----> oRequired = new required();
for (x in oRequired) {
var field = form[oRequired[x][0]];

if (field.type == 'text' ||
field.type == 'textarea' ||
field.type == 'file' ||
field.type == 'select-one' ||
field.type == 'radio' ||
field.type == 'password') {

var value = '';
// get field's value
if (field.type == "select-one") {
var si = field.selectedIndex;
if (si >= 0) {
value = field.options[si].value;
}
} else {
value = field.value;
}

if (trim(value).length == 0) {

if (i == 0) {
focusField = field;
}
fields[i++] = oRequired[x][1];
isValid = false;
}
}
}
if (fields.length > 0) {
focusField.focus();
alert(fields.join('\n'));
}
return isValid;
}

// Trim whitespace from left and right sides of s.
function trim(s) {
return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}
[ February 10, 2006: Message edited by: alan wamser ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have the following line in your JSP?

<html:javascript formName="transferForm"/>

If not, that's the problem. Just before the </html:form> is a good place to put it.
[ February 11, 2006: Message edited by: Merrill Higginson ]
 
alan wamser
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, that's how I found the javascript error...
 
Jeff Fischer
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After I PMed you to see whether you'd figured this out...I did! At least, I had the same problem, and this is how I solved it.

The root cause turned out to be that the copy of the static validator JavaScript code I was using was out of synch with the version of Struts Validator I was using. Apparently they changed the name of the JS dynamic method (the one that is injected into your form with the html:javascript tag) that performs the "required" field validation from simply "required()" to "<your form name here>_required()". Yet the static code (which I had long ago put into a library and referenced at run time) still expected the old method name.

So the solution was straightforward enough: I replaced my old static JS library file with a copy of the new static JS (load the page once with the staticJavascript option set to "true", view source, copy and paste), and voila! no more "'required' is undefined" error.

Hope this helps.
 
pranay hira
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam getting the same error but than with 'email undefined'.
Does any one has an idea what might be wrong? Everything is working validating on Integer, Double but email is not working:

 
Prakash Mani - Attur
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Fischer wrote: Apparently they changed the name of the JS dynamic method (the one that is injected into your form with the html:javascript tag) that performs the "required" field validation from simply "required()" to "<your form name here>_required()". Yet the static code (which I had long ago put into a library and referenced at run time) still expected the old method name.
I replaced my old static JS library file with a copy of the new static JS (load the page once with the staticJavascript option set to "true", view source, copy and paste), and voila! no more "'required' is undefined" error.

Hope this helps.

Yes as you said loaded script have method form_required() but they are calling form_requiredValidate()..
Where will be the JS library files to be replaced with new JS library files..
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic