• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

custom validation w/ Struts Validator

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I hope this question is not to remedial but I am really trying to understand what's wrong here.
I am trying to create a new validator for 'time'. I am basically parsing the string down to test components that make up the user input. I have followed numerous examples from books and internet to come up with the code below.
My new class seems to be the problem as I can get the client side to function and return a valid boolean but it will not give me the resource message either client side or server side. I am able to put alert message in my javascript and get those but it will not return with the .properties message I wrote. All other validation (date, required, etc.. etc..) works fine both client and server.
I included the code (minus the logic, I'm sure that is works) where I thought the problem may lie, but I'm new to Struts and fairly new to Java so if you need more I would be more than happy to oblige(just tell me what would help) but the rest of it seemed fairly standard and not where the problem may be.
Thanks in advance.
Jim

/************************************************************
new class I created

package com.gli.validator;

import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;
import org.apache.struts.action.ActionErrors;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.ValidatorUtil;
import org.apache.struts.util.StrutsValidatorUtil;

public class GLIValidator implements Serializable {
/**
*
**/
public GLIValidator(){
}

public static boolean validateTime(Object bean, ValidatorAction va, Field field,
ActionErrors errors, HttpServletRequest request) {

//processing to validate (left out to shorten email but it works)
// ValidatorUtil.getValueAsString(bean, field.getProperty() )
//
//


if ( !bValid )
{ errors.add(field.getKey(), StrutsValidatorUtil.getActionError(request, va, field)); }

return bValid;
}

}

***************************************************************/

/**************************************************************
relevant portion of validator-rules.xml

<validator name="time"
classname="com.gli.validator.GLIValidator"
method="validateTime"
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.time"
jsFunctionName="TimeValidations">

<javascript><![CDATA[
function validateTime(form){

// process to validate client side (left out to shorten email but it works)
return bValid;
} ]]>
</javascript>
</validator>
/**************************************************************

/**************************************************************
relevant portion of validation.xml

<form name="onCallLogForm">
<field property="call_date" depends="required,date">
<arg0 key="label.call_date"/>
<var>
<var-name>datePatternStrict</var-name>
<var-value>MM/dd/yyyy</var-value>
</var>
</field>
<field property="call_time" depends="required,time">
<arg0 key="label.call_time"/>
</field>
***************************************************************/
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it is worth posting your javascript code here, as it should contain more than logic that returns a boolean. Parts of it are important to the configuration.
 
John Sikes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I have included the javascript from the validation.xml at the bottom. I have something else to add that might help. If I put a System.out.println("something"); in my GLIValidator class at the beginning of the validateTime method I never see it in the log?

<validator name="time"
classname="com.gli.validator.GLIValidator"
method="validateTime"
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.time"
jsFunctionName="TimeValidations">

<javascript><![CDATA[
function validateTime(form)
{ //alert("testing time");
var bValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
oTime = new TimeValidations();
for (x in oTime)
{
var value = form[oTime[x][0]].value;
value = value.replace(/^\s*|\s*$/g,""); //trim all leading and trailing
var hasMeridian = false;
var re = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/;
if (!re.test(value)) { return false; }
if (value.toLowerCase().indexOf("p") != -1) { hasMeridian = true; }
if (value.toLowerCase().indexOf("a") != -1) { hasMeridian = true; }
var values = value.split(":");
if ( (parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23) ) { return false; }
if (hasMeridian) {
if ( (parseFloat(values[0]) < 1) || (parseFloat(values[0]) > 12) ) { return false; }
}
if ( (parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59) ) { return false; }
if (values.length > 2) {
if ( (parseFloat(values[2]) < 0) || (parseFloat(values[2]) > 59) ) { return false; }
}
return true;
}

} ]]>
</javascript>
</validator>
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everything looks pretty good.

I caught something on my second read of your first post that the error is not retrieved for client OR server side.

Are you using your .properties in other locations to verify that they are working ok???
 
John Sikes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, they are working. If I fail validation on purpose on the "canned" validation (date, int, short, etc.. etc..) I get the proper message(s) (from client or server) depending whether I have the struts html javascript tag in place for client side. I also get my ActionErrors from the validate method in the ValidatorForm class.
by the way, thanks for your help. I'm still new at this and I don't always know the technical verbage to communicate.
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I can tell,
oTime[x][1]
should hold the error message in the for loop.

I'm no expert in the javascript side of the validation framework, but from what I've seen I believe that the server side does not complain (or do the logger) because the request never gets sent to the server. That's good news. It means that your javascript validation is working but not throwing out the proper alert.

If I am not correct let me know how I am wrong and we can solve this.
 
John Sikes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the compiled source in a Validator-ized jsp it builds the main validate function for the form and from there it goes to whatever fields you have elected to validate. I can see that it has the field in its array to be validated and I know it's going through the client side validation by setting alerts at different locations w/in this piece of javascript. All the other 'canned' javascripts from the validation-rules.xml are exactly like this and return a true/false result to functions built by the Validator plugin. However, even if I take out the javascript tag (no client side Validator) I can cause the errors in the 'canned' methods that come w/ Validator but my new class never gets called. I guess what I don't understand is that I can see no difference in my new (time) validation and the ones that come w/ the Validator. The only thing I'm unsure of is the class I built. I built the class pretty much straight out of the O'Reilly book except using my logic and naming. hey thanks again for the help
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

Try making these changes and see if it works.

1. Change signature of validateTime() method


2. Update validator definition in validator-rules.xml


In short, change ActionErrors to ActionMessages.
Sheldon Fernandes
 
John Sikes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sheldon,
No luck. Would I need to change my return and from the validate method on my FormBean that is extended from ValidatorForm? I am overriding the method so that I can use both client and server side validation. Thanks for the help again. I know I'm new to most of this but this doesn't seem like it should be this hard...
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use ActionErrors on my framework validator methods.

I have been assuming the validations that do work are being attempted on the same page as the ones you claim do not work. If not, you probably did not call super.validate(...) inside of your validate method. That is the line that calls on the validation framework server-side.
 
John Sikes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fixed! Thanks for ya'lls help.
I removed some comments from a javascript function that was keeping it from hitting the server validation. That was "Doh!" number 1. Easy enough, right.
So, at that point I could see my debugging comments so I know my new validator class holding my new validation method was good and I was getting the .properties errors on my JSP.
Next 'retard-o' error was in my new javascript. I knew it was failing as I was hitting my action path forward and I could put debugging w/in my script and see where it failed and that it was returning false. Well that's all well and good but you have to put it somewhere.
Thanks Marc

From what I can tell,
oTime[x][1]
should hold the error message in the for loop.



if (error > 0)
{ focusField = form[oTime[x][0]];
fields[i++] = oTime[x][1];
bValid = false;}
else
{ bValid = true;}
and then you have to actually display the stinking error(s)
if (fields.length > 0)
{ focusField.focus();
alert(fields.join('\n'));
}
So, thanks guys for helping a rookie out. That'll learn me to frankestien some code without really and not really understanding what's going on.
 
Are we home yet? Wait, did we forget the tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic