Search code examples
spring-bootthymeleafspring-thymeleaf

Thymeleaf call a bean on a form field before submit


Let us suppose to have a bean:

@Bean
PasswordEncoder getPasswordEncoder()
{
  return new BCryptPasswordEncoder();
}

and on the html page:

 <div class="form-group">
        <label> Confirm Password</label>
        <input type="password" class="form-control" th:field="*{confirmPassword}">
    </div> 

Is there any way to call BCryptPasswordEncoder().someFunction() on th:field before its value is send through submit?


Solution

  • Simple answer, No.

    Thymeleaf is a rendering engine so it can only do things before the html is served to the end user. If you'd have a need to validate a field before submiting the form you either have to do validation in javascript or have some method to validate in the background against server using ajax.

    In most cases normal behaviour would be to do validation on form submit and if it is incorrect give feedback to user than