Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Struts Validator Framework: problem with Custom Validator

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michele Giunti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, server-side validation works!I changed ActionErrors class with ActionMessages class into my java validation method and into "validator-rules.xml".
The only problem is that
Resources.getActionMessage(request, va, field)method in my custom validator class is deprecated and I don't know how to replace it.
Can you give me any suggestions?


On the client-side, I have the same problem.I see a javascript warning in browser: "twofields is not defined".
I have also tried to perform some changes to my files:
**************
validator-rules.xml
**************

**********
validation.xml
**********


So, my custom validation ("twofields") doesn't still work on client-side.
Can you help me?
I also tried to change the version of dtd validator...but nothing changed.
I can't understand where is the problem.
 
Michele Giunti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No-reply?

However, I solved. Now all works well.

**********
SERVER-SIDE
**********
To avoid the deprecated method

I used

in my custom validator java class.
I added "org.apache.commons.validator.Validator" to methodParams of the "twofields" custom validator in validator-rules.xml.

**********
CLIENT-SIDE
**********
I changed

with




That's all.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!