I'm writing a simple calculator EP, here's the form that I'm using
@AllArgsConstructor
@NoArgsConstructor
@Data
public class CalculatorForm {
private double leftArg;
private double rightArg;
private String operation;
@AssertTrue(message = "You can't divide by zero!")
private boolean isValid(){
return !(operation.equals("/") && rightArg == 0);
}
public double getResult() {
leftArg = switch (operation) {
case "+" -> {
yield leftArg + rightArg;
}
case "-" -> {
yield leftArg - rightArg;
}
case "*" -> {
yield leftArg * rightArg;
}
case "/" -> {
// Assert.isTrue(rightArg != 0, "Can't divide by 0!");
yield leftArg / rightArg;
}
default -> throw new IllegalStateException("Unexpected operation: " + operation);
};
rightArg = 0;
return leftArg;
}
}
and now requesting zero division responds with error webpage but I want to use isValid() inside thymeleaf form so that it won't even be allowed to proceed with such request, how can I achieve that?
I've tried to follow this guide but it looks like it works only for fields while my validation is a method