You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Dirk Schumacher <di...@denkwerk.com> on 2008/03/14 15:00:41 UTC

Struts2 - Validating several fields depending on values of other fields

Hello out there,

I am relativley new to S2 and I got stuck on a validation problem.

2 fields/properties to be set on my Action:
startDate(String)
endDate(String)

When setting a value first of all the String must be parsed into a Date 
object. I know how to handle that.
The endDate must be after the startDate. And that's the problem.

I'd like to annotate the properties-setters, so an implementing 
validator takes care of the validation.

sth like this.:
@CustomizableS2Validator ( message="Insuffient Start Date", 
when="before", otherDate="endDate" )
public void setStartDate(String inDate) {
  date = inDate;
}

Implementing a FieldValidatorSupport does not work because I only know 
how to access the validated property. But I need another property of the 
Action object.

I know it would be solveable by the @FieldExpressionValidator where its 
expression can be filled with a complex OGNL-Expression.
But if evaluating the expression internally throws an Exception, the 
evaluation does not terminate right. (I'd be fine with a negative validation
and re-jecting to the input page, but if the OGNL is not evaluateable it 
passes the validation)

Another way is to validate within the executed action method. I want a 
Validator.
Yet, another is to implement a Validator which accesses Porperties via 
Reflection and analyzing the Annotations.

But I'd like to have a Validator object handling the validation concern.

Big question?
Is there even a way to do this.
Is it possible to customize the parameters of an annotation?
What do I have to replace for @CustomizableS2Validator

Best wondering greetings from cologne
Dirk

-- 
softwareentwickler

denkwerk gmbh | vogelsanger straße 66 | d-50823 köln
telefon +49 221 2942 100 | telefax +49 221 2942 101
GF: Axel Schmiegelow, Marco Zingler, Jochen Schlaier, Frank Zimpel
HRB 32063 Amtsgericht Köln  -  www.denkwerk.com


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


Re: Struts2 - Validating several fields depending on values of other fields

Posted by Laurie Harper <la...@holoweb.net>.
Dirk Schumacher wrote:
> I know it would be solveable by the @FieldExpressionValidator where its 
> expression can be filled with a complex OGNL-Expression.
> But if evaluating the expression internally throws an Exception, the 
> evaluation does not terminate right. (I'd be fine with a negative 
> validation
> and re-jecting to the input page, but if the OGNL is not evaluateable it 
> passes the validation)

The expression shouldn't be so complicated; you're just comparing two 
dates, after all. And why would the OGNL expression throw an exception? 
The only issue I would expect is with one or both dates being null, 
which you can avoid using a 'required' validator.

I would think working through any issues with this approach would be a 
lot less pain than going for a custom validator in this case.

L.


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


Re: Struts2 - Validating several fields depending on values of other fields

Posted by Dave Newton <ne...@yahoo.com>.
--- Dirk Schumacher wrote:
> 2 fields/properties to be set on my Action:
> startDate(String)
> endDate(String)
> 
> When setting a value first of all the String must be parsed into a Date 
> object.

FYI, that can be handled automatically by the framework via type conversion
[1] unless there's some compelling reason to explicitly convert from a
string.

> I'd like to annotate the properties-setters, so an implementing 
> validator takes care of the validation.
> 
> sth like this.:
> @CustomizableS2Validator ( message="Insuffient Start Date", 
> when="before", otherDate="endDate" )
> public void setStartDate(String inDate) {
>   date = inDate;
> }

IIRC using an annotation for a custom validator is a bit more complicated
than that because of how annotations work (they can't be subclassed) and
XWork sets things up with regards to validation annotations. You'd probably
have to use @CustomValidator [2].

> Is it possible to customize the parameters of an annotation?

@ValidationParameter [3] sets parameters for @CustomValidator. I find it
pretty unwieldy, but because of the way XWork looks for validation
annotations I'm not sure what the options are at this point. Perhaps somebody
else will have a better solution.

Dave

[1] http://struts.apache.org/2.x/docs/type-conversion.html
[2] http://struts.apache.org/2.x/docs/customvalidator-annotation.html
[3] http://struts.apache.org/2.x/docs/validationparameter-annotation.html



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