My poblem is connected with @AssertTrue annotation and some how with Thymeleaf. Actualy I should scheck password for the equality on a registration web page, for this I hava a parameters in my Registrartion Form class, they are Name,password, check_password,check_password_condition, adress. Actually I have made several changes in y code since I have aske my question here. I have used equals()
method instead of ==
and acording this post
Spring validation @AssertTrue
have set a validation property boolean check_password_condition
but my code still does not work. This way I use Errors
interface to ask my page for validation rules. I think that using of the annotation @AssertTue for my method sould automatically call my method isCheckpassword()
from RegistrationForm
class and then in @PostMapping
method of the Controller asked for the validation rule this.password.equals(this.check_password)
Am I right or not????
@AssertTrue(message = "{RegistrationForm.check_password.AssertTrue}")
public boolean isCheckpassword(){
if(this.password.equals(this.check_password)){
return this.check_password_condition=true;
}
else return this.check_password_condition=false;
}
@PostMapping("/register")
String registerNew(@Valid RegistrationForm form, Errors result) {
if (result.hasErrors()) {
return "register";
}
customerManagement.createCustomer(form);
return "redirect:/";
}
But I get whitepage error when the conditions for the creating new user are not met.
here additionally I provide my Thymeleaf code:
<div class="field">
<label th:text="#{register.check_password}" for="check_password">Repasswort</label>
<input id="check_password" name="check_password" th:field="*{check_password}" th:errorclass="fieldError" type="password"
required="required"/><br/>
<p th:if="${#fields.hasErrors('check_password')}" th:errors="*{check_password}">Das Passwort darf
nicht leer sein.</p>
</div>
This is my Registration From class
class RegistrationForm {
@NotEmpty(message = "{RegistrationForm.name.NotEmpty}") //
private final String name;
@Size(min = 2, max = 14, message = "{RegistrationForm.password.Size}")
@NotEmpty(message = "{RegistrationForm.password.NotEmpty}") //
private final String password;
@NotEmpty(message = "{RegistrationForm.check_password.NotEmpty}") //
private String check_password;
private boolean check_password_condition;
@NotEmpty(message = "{RegistrationForm.address.NotEmpty}") // s
private final String address;
public RegistrationForm(String name, String password,String check_password, String address) {
this.name = name;
this.password = password;
this.check_password=check_password;
this.address = address;
}
@AssertTrue(message = "{RegistrationForm.check_password.AssertTrue}")
public boolean isCheckpassword(){
if(this.password.equals(this.check_password)){
return this.check_password_condition=true;
}
else return this.check_password_condition=false;
}
//return this.password != null && this.password.equals(this.check_password) : this.setCheck_password(); }
public String getName() {
return name;
}
public String getPassword() { return password; }
public String getCheck_password(){return check_password;}
public String getAddress() {
return address;
}
}
Please help to solve this problem when error info from Whitelabel errorpage is:
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('*')" (template: "register" - line 29, col 42)
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290)
at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:125)