struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jerry Jalenak" <Jerry.Jale...@LABONE.com>
Subject RE: how do i get a Field object in Validator?
Date Wed, 23 Oct 2002 13:17:16 GMT
OK.  I had several similar requirements and got around them by building an
error message for field 'B' on the fly. 

application-rules.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD
Struts Validator Rules Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/validator-rules_1_1.dtd"-->

<form-validation>
	<global>
		<validator name="validateMVR" 
	
classname="com.labone.struts.extensions.ValidatorExtensions"
				method="ValidateMVR" 
				methodParams="java.lang.Object,
	
org.apache.commons.validator.ValidatorAction,
	
org.apache.commons.validator.Field,
	
org.apache.struts.action.ActionErrors,
	
javax.servlet.http.HttpServletRequest"
				msg="errors.field.error" />
	</global>
	<formset>
		<form name="/InsertAction">
			<field property="mvrSelected" depends="validateMVR">
				<var>
					<var-name>mvrAccount</var-name>
					<var-value>mvrAccount</var-value>
				</var>
			</field>
		</form>
	</formset>
</form-validation>

<code-snip>
	public static boolean ValidateMVR(Object object, ValidatorAction va,
Field field, ActionErrors errors, HttpServletRequest request)
	{
		String mvrSelected = ValidatorUtil.getValueAsString(object,
field.getProperty());  // field to check
		if (!(mvrSelected.equals("false")))
		{
			String mvrAccount =
ValidatorUtil.getValueAsString(object, field.getVarValue("mvrAccount"));  //
second variable to check
			if (mvrAccount.equals("notSelected"))
			{
				field.setKey("mvrAccount");  // set the
field name for the error object; error message is 'errors.field.error'
				Arg arg0 = new Arg();
	
arg0.setKey("InsertForm.account.displayname");  // insert arg0 (field name)
				field.addArg0(arg0);
				Arg arg1 = new Arg();
				arg1.setKey("text.required");  // insert
arg1 (is required)
				field.addArg1(arg1);
				errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
				return false;
			}
		}
		return true;
	}
</code-snip>

ApplicationResources.properties (for 'errors.field.error')

errors.header=<span style="color:FF0000;font-family:Verdana;font-size:9pt;">
errors.footer=</span>
errors.prefix=
errors.suffix=

# Struts Validator Error Messages - do not change!

errors.required={0} is required.<br>
errors.minlength={0} can not be less than {1} characters.<br>
errors.maxlength={0} can not be greater than {1} characters.<br>
errors.invalid={0} is invalid.<br>
errors.byte={0} must be an byte.<br>
errors.short={0} must be an short.<br>
errors.integer={0} must be an integer.<br>
errors.long={0} must be an long.<br>
errors.float={0} must be an float.<br>
errors.double={0} must be an double.<br>
errors.date={0} is not a date.<br>
errors.range={0} is not in the range {1} through {2}.<br>
errors.creditcard={0} is not a valid credit card number.<br>
errors.email={0} is an invalid e-mail address.<br>

# locally defined messages for validator

errors.field.error={0} {1}<br>

text.field=Field 
text.required=is required.
text.invalid=contains invalid character(s).
text.format=format is invalid for selected state.

InsertForm.account.displayname = Account Number

If I need to I can change the errors.field.error to have up to three or four
placeholders; using this approach gives me the flexability to create custom
error messages (provided I've though about what I want to tell the user in
advance!).

HTH,

Jerry


> -----Original Message-----
> From: Andy Kriger [mailto:akriger@greaterthanone.com]
> Sent: Tuesday, October 22, 2002 3:13 PM
> To: Struts Users Mailing List
> Subject: RE: how do i get a Field object in Validator?
> 
> 
> Essentially, yes. Lets say I have 2 fields A & B. I am 
> validating A but it
> requires B. If B fails, I'd like to get the arg0 value from 
> B's Field object
> and use that in the error message (since there's a key to the 
> display name
> in that arg).
> 
> I've worked around this by passing in keys as a separate var 
> but like I said
> earlier, it's ugly.
> 
> -----Original Message-----
> From: Jerry Jalenak [mailto:Jerry.Jalenak@LABONE.com]
> Sent: Tuesday, October 22, 2002 15:29
> To: 'Struts Users Mailing List'
> Subject: RE: how do i get a Field object in Validator?
> 
> 
> Not quite sure I understand your question then.  Are you wanting to
> manipulate the error message that is being returned?  In 
> other words, if one
> of the 'other' variables fails, then return a message for 
> that failure ?
> 
> Jerry
> 
> > -----Original Message-----
> > From: Andy Kriger [mailto:akriger@greaterthanone.com]
> > Sent: Tuesday, October 22, 2002 1:45 PM
> > To: Struts Users Mailing List
> > Subject: RE: how do i get a Field object in Validator?
> >
> >
> > That much I understand - but is it possible to get a
> > reference to the Field
> > objec that represents that 2nd property? For example, if you
> > wanted to get
> > message arguments from the other field.
> >
> > -----Original Message-----
> > From: Jerry Jalenak [mailto:Jerry.Jalenak@LABONE.com]
> > Sent: Tuesday, October 22, 2002 14:19
> > To: 'Struts Users Mailing List'
> > Subject: RE: how do i get a Field object in Validator?
> >
> >
> > In your validator-rules.xml file, add the following for each
> > form field you
> > need access to:
> >
> > 	<form name="yourForm">
> > 		<field property="firstProperty"
> > depends="yourCustomRoutine">
> > 			<var>
> >
> > <var-name>secondProperty-Label</var-name>
> >
> > <var-value>secondProperty-Value</var-name>
> > 			</var>
> > 		</field>
> > 	</form>
> >
> > secondProperty-Label would be what you want to call the
> > variable in your
> > custom routine; secondProperty-Value would be the actual form
> > field name
> > that contains the entered value.  Then in your custom routine
> > you can access
> > these values by
> >
> > 	public static boolean yourCustomRoutine(Object object,
> > ValidatorAction va, Field field, ActionErrors errors,
> > HttpServletRequest
> > request)
> > 	{
> > 		String fp = ValidatorUtil.getValueAsString(object,
> > field.getProperty());
> > 		String sp = ValidatorUtil.getValueAsString(object,
> > field.getVarValue("secondProperty-Label"));
> >
> > 		... do something ...
> > 		return true;  // or false if the validation fails
> > 	}
> >
> > HTH,
> >
> > Jerry
> >
> > > -----Original Message-----
> > > From: Andy Kriger [mailto:akriger@greaterthanone.com]
> > > Sent: Tuesday, October 22, 2002 11:33 AM
> > > To: Struts Users Mailing List
> > > Subject: how do i get a Field object in Validator?
> > >
> > >
> > > I am writing a custom rule and I need to know how I can get a
> > > handle to a
> > > different Field object? (not the one passed into the method
> > > in the first
> > > place)
> > >
> > > thx
> > > a
> > >
> > >
> > >
> > > --
> > > To unsubscribe, e-mail:
> > > <mailto:struts-user-unsubscribe@jakarta.apache.org>
> > > For additional commands, e-mail:
> > > <mailto:struts-user-help@jakarta.apache.org>
> > >
> > >
> >
> > This transmission (and any information attached to it) may be
> > confidential
> > and is intended solely for the use of the individual or
> > entity to which it
> > is addressed. If you are not the intended recipient or the person
> > responsible for delivering the transmission to the intended
> > recipient, be
> > advised that you have received this transmission in error and
> > that any use,
> > dissemination, forwarding, printing, or copying of this 
> information is
> > strictly prohibited. If you have received this transmission
> > in error, please
> > immediately notify LabOne at (800)388-4675.
> >
> >
> >
> > --
> > To unsubscribe, e-mail:
> > <mailto:struts-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> > <mailto:struts-user-help@jakarta.apache.org>
> >
> >
> >
> > --
> > To unsubscribe, e-mail:
> <mailto:struts-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:struts-user-help@jakarta.apache.org>
> 
> 
> This transmission (and any information attached to it) may be 
> confidential
> and is intended solely for the use of the individual or 
> entity to which it
> is addressed. If you are not the intended recipient or the person
> responsible for delivering the transmission to the intended 
> recipient, be
> advised that you have received this transmission in error and 
> that any use,
> dissemination, forwarding, printing, or copying of this information is
> strictly prohibited. If you have received this transmission 
> in error, please
> immediately notify LabOne at (800)388-4675.
> 
> 
> 
> --
> To unsubscribe, e-mail:
> <mailto:struts-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:struts-user-help@jakarta.apache.org>
> 
> 
> 
> --
> To unsubscribe, e-mail:   
> <mailto:struts-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: 
> <mailto:struts-user-help@jakarta.apache.org>
> 
> 

This transmission (and any information attached to it) may be confidential and is intended
solely for the use of the individual or entity to which it is addressed. If you are not the
intended recipient or the person responsible for delivering the transmission to the intended
recipient, be advised that you have received this transmission in error and that any use,
dissemination, forwarding, printing, or copying of this information is strictly prohibited.
If you have received this transmission in error, please immediately notify LabOne at (800)388-4675.



--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@jakarta.apache.org>


Mime
View raw message