Frage

I am looking to have specific behaviour on my validate() method (like the one I can have with the groups annotation) either if it's called on

Form<User> loginForm = form(User.class, User.Login.class).bindFromRequest();` 

or on

Form<User> registerForm = form(User.class, User.Register.class).bindFromRequest();

User Model :

@Entity
public class User extends Model {

    public interface Register {}
    public interface Login{}        

    @Required(groups = {Register.class, Login.class})
    public String username;

    @Required(groups = {Register.class, Login.class})
    public String password;

    public List<ValidationError> validate() {       
       ... // Here I would like to distinguish User.Login.class from User.Register.class
    }
}

Application Controller

public static Result loginSubmit(){
    Form<User> loginForm = form(User.class, User.Login.class).bindFromRequest();
}

public static Result registerSubmit(){
    Form<User> registerForm = form(User.class, User.Register.class).bindFromRequest();
}
War es hilfreich?

Lösung

The group parameter isn't passed to the validate() method so I don't think that's possible. It's not as convenient but you could just call validate (or some other validation method) yourself.

User Model:

public class User extends Model
{
  public List<ValidationError> validate(Class group) {
    if (group == Login.class) {
      ...
    } else if (group == Register.class) {
      ...
    }
  }
}

Controller:

public static Result loginSubmit(){
  Form<User> loginForm = form(User.class, User.Login.class).bindFromRequest();
  if (!loginForm.hasErrors()) {
    User user = loginForm.get();
    List<ValidationError> errors = user.validate(User.Login.class);
    ...
  }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top