title: How to validate min, max password length before hashing?
body: <p>I want to validate the password field length before it's hashed.</p>
  
  <p><strong>Model class:</strong></p>
  
  <pre><code>@Entity
  @Table(name = "users")
  public class UserInfo {
  
     /* other code */
  
     @NotBlank(message = "Password is required")
     @Size(min = 6, message = "Password should have min 6 characters")
     private String password;
  
     /* other code */    
  }
  </code></pre>
  
  <p>Controller file simply calls the service method.</p>
  
  <p><strong>Service class:</strong></p>
  
  <pre><code>@Component
  public class UserInfoServiceImpl implements UserInfoService {
  
  @Autowired
  private UserInfoRepository userInfoRepository;
  
  public UserInfo register(UserRegisterRequest request) {
  
      UserInfo user = new UserInfo();
      user.setFirstName(request.getFirstName());
      user.setLastName(request.getLastName());
      user.setEmail(request.getEmail());
      user.setPhone(request.getPhone());
      // Password hashing
      user.setPassword(new BCryptPasswordEncoder().encode(request.getPassword()));
      user.setIsActive(0);
      user.setStatus(1);
  
      return userInfoRepository.save(user);
    }
  }
  </code></pre>
  
  <p>I suppose the password is validated after it's hashed in this line:</p>
  
  <pre><code>user.setPassword(new BCryptPasswordEncoder().encode(request.getPassword()));
  </code></pre>
  
  <p><strong>How can I validate this password before hashing and saving?</strong></p>
  
  <p>Any help would be appreciated. Thanks in advance.</p>