Two Laptop Bag*
The moose likes Struts and the fly likes Error 'required' is undefined Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Error Watch "Error New topic
Author

Error 'required' is undefined

alan wamser
Ranch Hand

Joined: Jan 30, 2006
Posts: 41
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

Joined: Jan 30, 2006
Posts: 41
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

Joined: Feb 15, 2005
Posts: 4864
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 ]

Merrill
Consultant, Sima Solutions
alan wamser
Ranch Hand

Joined: Jan 30, 2006
Posts: 41
Yep, that's how I found the javascript error...
Jeff Fischer
Greenhorn

Joined: Feb 28, 2006
Posts: 1
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

Joined: Jun 26, 2006
Posts: 15
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:



-------------<br />SCJA<br />SCJP 1.4/5.0
Prakash Mani - Attur
Ranch Hand

Joined: Oct 08, 2009
Posts: 100
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..
 
Consider Paul's rocket mass heater.
 
subject: Error 'required' is undefined
 
Similar Threads
Form validation in struts
Validation failure
server side validation errors
STRUTS: client-side validation in jsp using DynaValidatorForm
validation