You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by mraible <ma...@raibledesigns.com> on 2007/08/03 22:39:03 UTC

Re: FieldExpressionValidator: How do I reference field names?

Just to follow up, I've created my own custom FieldExpressionValidator to get
my desired behavior. The code is below in case anyone wants to use it.
Here's how:

1. Create a validators.xml in your WEB-INF/classes (or src/main/resources
for M2) to override the "fieldexpression" validator:

<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">

<validators>
  <validator name="fieldexpression"
class="com.yourcompany.app.validation.LogicalFieldExpressionValidator"/>
</validators>

2. Create LogicalFieldExpressionValidator:

package com.yourcompany.app.validation;

import
com.opensymphony.xwork2.validator.validators.FieldExpressionValidator;
import com.opensymphony.xwork2.validator.ValidationException;

/**
 * Override XWork's FieldExpressionValidator because it's more intuitive to
 * write expressions that evaluate to false rather than true.
 */
public class LogicalFieldExpressionValidator extends
FieldExpressionValidator
{
  public void validate(Object object) throws ValidationException
  {
    String fieldName = getFieldName();

    Boolean answer = Boolean.FALSE;
    Object obj = null;

    try
    {
      obj = getFieldValue(getExpression(), object);
    }
    catch (ValidationException e)
    {
      throw e;
    }
    catch (Exception e)
    {
      // let this pass, but it will be logged right below
    }

    if ((obj != null) && (obj instanceof Boolean))
    {
      answer = (Boolean) obj;
    }
    else
    {
      log.warn("Got result of " + obj + " when trying to get Boolean.");
    }

    // parent class has !answer.booleanValue()
    if (answer.booleanValue())
    {
      addFieldError(fieldName, object);
    }
  }
}

After doing this, instead of having the following expression:

    <field name="friendEmail">
        <field-validator type="fieldexpression">
            reason != null and (reason == 'friend' and friendEmail == '')
            <message>Please provide your friend's email</message>
        </field-validator>
    </field>

I get to have:

    <field name="friendEmail">
        <field-validator type="fieldexpression">
            reason == 'friend' and friendEmail == ''
            <message>Please provide your friend's email</message>
        </field-validator>
    </field>

This seems much more logical to me.

Matt




mraible wrote:
> 
> 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: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/FieldExpressionValidator%3A-How-do-I-reference-field-names--tf4104715.html#a11989689
Sent from the Struts - User mailing list archive at Nabble.com.


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