0

I have a fairly complex and big Model called Task. In this object I have some heavy custom logic and date time conversion so I decided to make a POJO for the form since I need custom validation / conversion. But how to bind this POJO back to a Model?

Is this the correct way:

public static Result save() {
    Form<forms.Task> taskForm = form(forms.Task.class).bindFromRequest();

    if (taskForm.hasErrors()) {
        return badRequest(views.html.tasks.create.render(taskForm));
    }
            // bind the formValues to our model
    Task newTask = form(Task.class).bind(taskForm.data()).get();
    newTask.createdBy = User.getLoggedInUserByAlias(session().get("user"));
    newTask.save();

    return GO_TO_OVERVIEW;
}

Is there a cleaner way to do it? I think that I am doing this wrong.

4

1 に答える 1

0

ウィキで述べたように: https://github.com/playframework/Play20/wiki/JavaFormsメソッドを 使用したカスタム POJO は、使用するvalidateのに最適で正しいものです。

public class User {

  @Required
  public String email;
  public String password;

  public String validate() {
      if(authenticate(email,password) == null) {
          return "Invalid email or password";
      }
      return null;
  }
}
于 2012-07-27T15:15:43.243 に答える