The following expression seems to do the trick:
reason != 'friend' or (reason == 'friend' and friendEmail != '')
However, this doesn't seem very intuitive, does it? Writing it in Java seems
more logical:
public void validate() {
if (reason != null && reason.equals("friend") && friendEmail.equals(""))
{
addFieldError("friendEmail", "Please provide your friend's email");
}
}
Why does the expression use the opposite (friendEmail != "") where the Java
uses friendEmail == ""? Doesn't that seem confusing?
Matt
Eric Rank-2 wrote:
>
>
> My Bad, there's another scenario when this field will validate. When
> reason != 'friend'
>
> Better expression:
>
> (reason != 'friend') or ((reason ==
> 'friend') and (friendEmail != null) and (friendEmail.trim().size() >
> 0))
>
> Eric
>
> On Jul 19, 2007, at 1:33 PM, Eric Rank wrote:
>
>> Hi Matt,
>>
>> I tried out your scenario, and I think I found the problem. In my
>> test, it also validated when I left the friendEmail field blank. It
>> seems that the value of friendEmail is not null, but an empty
>> string. To solve the problem, I added another clause to check for
>> String length. After that, it triggered the field error as desired.
>> This is what worked for me.
>>
>> <validators>
>> <field name="friendEmail">
>> <field-validator type="fieldexpression">
>> reason == 'friend' and
>> friendEmail != null and friendEmail.trim().size() > 0
>> <message>Please provide your friend's email</message>
>> </field-validator>
>> </field>
>> </validators>
>>
>>
>> Eric.
>>
>>
>> On Jul 19, 2007, at 10:40 AM, mraible wrote:
>>
>>> If you're right, I'd expect the following expression make friendEmail
>>> required when the "friend" reason is checked (it's a radio button):
>>>
>>> reason == 'friend' and friendEmail != null
>>>
>>> However, if I check friend and don't fill out the e-mail address,
>>> it still
>>> passes validation. Based on the error message I'm getting in my
>>> logs (see
>>> below), I'm guessing that I need to do some sort of "friendEmail !
>>> = null"
>>> check, but I'm already doing that.
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
--
View this message in context:
http://www.nabble.com/FieldExpressionValidator%3A-How-do-I-reference-field-names--tf4104715.html#a11749020
Sent from the Struts - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]