I had used Hibernate validator with Spring, and the model "Admin" I need to check like this:
@NotEmpty
@Size(min = 4, max = 40)
private String password;
when I save "admin" the password is required, and I used "@Valid @ModelAttribute Admin admin". So it will be checked before save.
But when I update the admin, if the user didn't provide the password, we think that he didn't want to change password, so the null value for the password is legal, for this situation, we couldn't use "@Valid @ModelAttribute Admin admin" anymore, because the password property have been annotation with @NotEmpty, so for this situation, our approach is do by ourselves in the code manually, I suppose if is there a better way to solve? and we still want to use the Bean Validation feature.
And also another issue:
@NotEmpty
@Size(min = 2, max = 20)
private String username;
when we save username, we must check the username whether exists or not, that we need to check the DB, and this situation how to solve if we still use the bean validation, defined a
class level annotation?
I hope anyone could help me. Thanks
CreateAdminCommand
with the properties and validations for that useless. AndUpdateAdminCommand
/UpdateAdminRequest
(or whatever you like) with the properties and validations for that usecase. Gives you intend in your domain and you can have a service method handling those different usecases. The only drawback is you need to copy some data around, the advantage is clarity and you protect your domain. – M. Deinumempty or when present values must bear certain rules
this is too much. – bhantol