You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Daniel Lipofsky <da...@bricsnet.com> on 2004/12/29 03:36:23 UTC

Validate only if existing

I have a field I want to require only if the bean
it exists inside exists (is not null).

I cannot use validwhen because I am using struts 1.1.

I tried to use requiredif like this

	<form name="/ChargeTypeStore">
		<field property="chargeTypeDTO.name"
depends="requiredif">
			<arg0 key="LeaseChargeTypeDTO.name"/>
			<var>
				<var-name>field[0]</var-name>
				<var-value>chargeTypeDTO</var-value>
			</var>
			<var>
				<var-name>fieldTest[0]</var-name>
				<var-value>NOTNULL</var-value>
			</var>
		</field>
	</form>

but I get

org.apache.commons.beanutils.NestedNullException:
Null property value for 'chargeTypeDTO'

I want to require chargeTypeDTO.name if and only if
chargeTypeDTO is not null.  Can I do this in struts 1.1?
Thanks,
Dan


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


Re: Validate only if existing

Posted by Erik Weber <er...@mindspring.com>.
The idea of requiredif is to require a value for a field depending on 
whether there is/isn't a value for another *field*. I'm not sure if you 
can use it to hinge on other (non-field) values.

For example, if a user lives in an apartment, you want him to indicate 
his apartment number. You have a checkbox field "isApartmentDweller" and 
with it a text field "apartmentNo". If the isApartmentDweller field 
value is true (checked), you want to require a value for apartmentNo. If 
the isApartmentDweller field value is false or null (unchecked), you 
don't want to require a value for apartmentNo. So you use requiredIf on 
apartmentNo to test for the presence of a value ("true") on the 
isApartmentDweller field.

It appears to me that you are trying a hack here (an unintended use of 
requiredif), unless I'm not understanding you (sorry if I'm not). I'm 
not sure if you will have any success.

But, I think there are other ways to solve your problem, assuming that 
you know if the DTO reference has a value at page rendering time.

You could make the field simply required, but only render the field if 
the DTO has a value. Then you could set the action URL, using a 
variable, differently, depending on whether this value is present, at 
page rendering time. The different action URLs are associated with 
different (but similar) form beans -- each with its own set of 
validation rules. One set of validation rules requires the field, the 
other doesn't. This solution is elegant (especially for reusing the same 
JSP for adds and updates where fields vary somewhat between add and 
update), but obviously requires a little work if you aren't already 
doing something like this.

Alternatively (I haven't tried this), you could set the value of a 
hidden field according to whether the DTO has a value at page rendering 
time. Then your requiredif rule could hinge on the value of the hidden 
field.

Hope that helps,
Erik



Daniel Lipofsky wrote:

>I have a field I want to require only if the bean
>it exists inside exists (is not null).
>
>I cannot use validwhen because I am using struts 1.1.
>
>I tried to use requiredif like this
>
>	<form name="/ChargeTypeStore">
>		<field property="chargeTypeDTO.name"
>depends="requiredif">
>			<arg0 key="LeaseChargeTypeDTO.name"/>
>			<var>
>				<var-name>field[0]</var-name>
>				<var-value>chargeTypeDTO</var-value>
>			</var>
>			<var>
>				<var-name>fieldTest[0]</var-name>
>				<var-value>NOTNULL</var-value>
>			</var>
>		</field>
>	</form>
>
>but I get
>
>org.apache.commons.beanutils.NestedNullException:
>Null property value for 'chargeTypeDTO'
>
>I want to require chargeTypeDTO.name if and only if
>chargeTypeDTO is not null.  Can I do this in struts 1.1?
>Thanks,
>Dan
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
>
>  
>

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