|
From: Oliver H. <Ol...@ou...> - 2005-09-12 22:47:26
|
=20
> Basically we could do something like this:
>=20
>=20
> public @interface Validate {
> String constraint();
> String code();
> String message();
> String[] arguments();
> }
I personally think it would be nicer to implement the annotation in a
way that is consistent with the rule definitions used by
ValangValidatorFactoryBean:
public @interface Validate {
String valang();
}
public class MyClass {
@Validate("? not blank : 'First name should not be blank' :
'firstName_blank'")
public String getFirstName() { return this.firstName; }
Also how would you deal with validating nested objects? For instance,
when editing contact details, our website will present the user with a
list of states and also with the option of entering in an alternative
state not in the list using a text field. Usually we bind directly onto
our domain object but because we have introduced an additional field I
create a form object that provides an accessor for the domain object and
for the additional field and then bind what I can directly to the domain
object and the additional fields onto the form object:
class ContactForm {
Contact contact;
String otherState;
// where possible bind directly to this domain object
getContact() { return contact;}=20
@Validate("? is not blank && contact.state is not blank :
'Please select or enter a state but not both')
String getOtherState() { return otherState; }
void setOtherState(String otherState) {this.otherState =3D
otherState; }=09
}
How would you tell the annotation driven validator that it must also
validate the Contact object? Would we need an additional annotation?
public @interface ValidateNested {
}
class ContactForm {
Contact contact;
String otherState;
@ValidateNested
getContact() { return contact;}=20
...
Ollie
|