How can I go about overriding the validation on the email for the AuthorizedUser in the following situation:
public class Account {
@Length(min = 1, max = 100,
message = "'Email' must be between 1 and 100 characters in length.")
@NotNull(message = "'Email' must not be empty.")
protected String email;
@Length(min = 1, max = 50,
message = "'Name' must be between 1 and 50 characters in length.")
private String name;
}
public class AuthorizedUser extends Account {
@Length(min = 1, max = 40,
message = "'Field' must be between 1 and 50 characters in length.")
private String field;
}
I know I could 'hack' the solution by overriding the email address in the setter on the AuthorizedUser by doing the following:
@Override
public void setEmail(String email) {
this.email = email;
super.setEmail(" ");
}
It just feels dirty... Is this possible to be overridden without writing a custom validator?
I tried moving the @Valid to the setter in the super class, and leaving it off in the overridden field, but I still receive the message from the super class about it being empty. Is there a lazier way to do this?
JSR 303 - Bean Validation - defines a metadata model and API for entity validation. The default metadata source is annotations, with the ability to override and extend the meta-data through the use of XML. The API is not tied to a specific application tier or programming model.
To avoid duplication of these validations in each layer, developers often bundle validation logic directly into the domain model, cluttering domain classes with validation code which is really metadata about the class itself. JSR 303 - Bean Validation - defines a metadata model and API for entity validation.
JSR-303 bean validation is an specification whose objective is to standardize the validation of Java beans through annotations. The objective of the JSR-303 standard is to use annotations directly in a Java bean class.
JSR 349 is the specification for Bean Validation 1.1. JSR 303 is the specification for Bean Validation 1.0 Bean validation is an important task irrespective of the architecture (enterprise web application or thick client-server) of the application. JSR 303 specification standardize the bean validation process.
Since constraints are aggregated through inheritance, the best solution may be to change your inheritance hierarchy to something like this:
public class BasicAccount {
protected String email;
@Length(min = 1, max = 50,
message = "'Name' must be between 1 and 50 characters in length.")
private String name;
}
public class EmailValidatedAccount extends BasicAccount {
@Length(min = 1, max = 100,
message = "'Email' must be between 1 and 100 characters in length.")
@NotNull(message = "'Email' must not be empty.")
@Override
public String getEmail() {
return email;
}
}
public class AuthorizedUser extends BasicAccount {
@Length(min = 1, max = 40,
message = "'Field' must be between 1 and 50 characters in length.")
private String field;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With