I'm using
Struts 1.3.
I'm trying to write a custom validator to check that 2 fields are identical.
1) I modified my validator-rules.xml by adding my own validator (called "uguale"):
**************
validator-rules.xml
**************
[CODE<validator name="uguale"
classname="it.sfidiamoci.validator.ValidatorPersonalizzati"
method="validateUguale"
methodParams="java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionErrors,javax.servlet.http.HttpServletRequest"
depends="required"
msg="errors.uguale">
<javascript><![CDATA[
function validateUguale(form) {
var bValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
oTwoFields = new twofields();
for (x in oTwoFields) {
var field = form[oTwoFields[x][0]];
var secondField = form[oTwoFields[x][2]("secondProperty")];
if (field.type == 'text' ||
field.type == 'textarea' ||
field.type == 'select-one' ||
field.type == 'radio' ||
field.type == 'password') {
var value;
var secondValue;
// get field's value
if (field.type == "select-one") {
var si = field.selectedIndex;
value = field.options[si].value;
secondValue = secondField.options[si].value;
} else {
value = field.value;
secondValue = secondField.value;
}
if (value != secondValue) {
if (i == 0) {
focusField = field;
}
fields[i++] = oTwoFields[x][1];
bValid = false;
}
}
}
if (fields.length > 0) {
focusField.focus();
alert(fields.join('\n'));
}
return bValid;
}]]></javascript>
</validator>[/CODE]
2) I modified my validation.xml by adding the new validator (called "uguale"):
**************
validator-rules.xml
**************
[CODE<field property="password" depends="required,minlength,mask,uguale">
<arg position="0" key="errors.password" />
<arg position="1" name="minlength" key="${var:minlength}" resource="false" />
<msg name="mask" key="errors.mask.password" />
<msg name="uguale" key="errors.password.uguale" />
<var>
<var-name>minlength</var-name>
<var-value>8</var-value>
</var>
<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z0-9_.]*$</var-value>
</var>
<var>
<var-name>secondProperty</var-name>
<var-value>password2</var-value>
</var>
</field>[/CODE]
3) I added the key "errors.password.uguale" in ApplicationResources.properties
***********************
ApplicationResources.properties
***********************
4) I created the class "ValidatorPersonalizzati" to handle the custom validator "validateUguale":
********************
ValidatorPersonalizzati.java
********************
5) I have two fields ("password", "password2") to check in my
jsp:
******
test.jsp
******
[CODE<tr>
<td class="col1"><bean:message key='registrazioneUtente.password.label'/></td>
<td><html :-P assword property="password" styleId="password" maxlength="<%= passwordMaxLength %>" /></td>
<td><bean:message key='registrazioneUtente.password2.label'/></td>
<td><html :-P assword property="password2" styleId="password2" maxlength="<%= passwordMaxLength %>" /></td>
</tr>[/CODE]
When I submit the form in my jsp, standard validations (e.g. required, minlength, ...) of other fields are performed in the correct way.
Instead, my own validation called "uguale" ("password==password2") doesn't work!
On the client-side, I see a javascript warning in browser: "twofields is not defined" but no javascript alert.
On the server-side, I get the following exception:
In the log I have left 2 System.out.println to be sure that "value" and "value2" variables had the correct value submitted through the form.
Moreover, I'm not sure that
<i>Resources.getActionMessage(request, va, field)</i>
method in my custom validator class is correct. This method is deprecated and I don't know how to replace it.
I can't understand where is the problem.
Can you help me?
Thanks in advance.