Say I have a simple form with the following field the user has to fill in:
name
surname
birth date
address
The data needs to be saved in a database table with the same fields/columns. All the columns are not nullable.
The form offers two buttons: "Save" and "Save as draft" to let the user finish compiling the form later.
The first will check if all the fields are correctly filled whereas the second doesnt make any check other than "cannot insert number in name field".
I tend to use Hibernate Validation annotations on my DTO, but in this case will break the function "Save as draft".
How would you handle this scenario?
Technologies I'm using: Spring Boot/MVC to expose the REST service consumed by frontend, hibernate/Spring DATA to save data to database.
CodePudding user response:
Create an interface for your entry and create two implementations of that interface.
One being the actualEntry with the annotations.
The other being the draft without those annotations.
CodePudding user response:
You can use a validation function instead of annotations directly on the attributes. That way your validation can have some logic in it.
@Value
class MyDto{
String name;
String surname;
DateTime birthdate;
Address asdress;
boolean draft;
@AssertTrue
private boolean validateInstance(){
if(!isDraft()){
// do validation
}
return true;
}
}