Confused About Play2 Form Submission (login/register)
This might seem very basic, but I am new to JPA/Ebean and Play Framework
(not so new with Java btw).
I'm trying to make two forms. One to log in and one to register. I'm using
both the book Play for Java MEAP (Early Access), the official website
documentation, and the sample app "ZenTask" codes. I must say Play is
strong, robust, but the documentation is so poor. I get this must have
something to do with most Play users are seasoned Java Web developers, but
still!
I created a model called User
@Entity
@Table(name="account")
public class User extends Model {
@Id
@Constraints.Required
@Formats.NonEmpty
public String email;
@Constraints.Required
public String displayName;
@Constraints.Required
public String password;
public static Model.Finder<String, User> find = new
Model.Finder(String.class, User.class);
public static User authenticate(String email, String password) {
return find.where()
.eq("email", email)
.eq("password", password)
.findUnique();
}
}
Then I have this controller Application
public class Application extends Controller {
public static class Login {
@Constraints.Required
public String email;
public String password;
public String validate() {
if (User.authenticate(email,password)==null) {
return "Invalid email or password.";
}
return null;
}
}
public static class Register {
@Constraints.Required
public String email;
public String password;
public String cfmPassword;
public String displayName;
public String validate() {
if (cfmPassword.equals(password)) {
return "Passwords typed in does not match.";
} else if (displayName.contains(" ")) {
return "Display name cannot contain space";
}
return null;
}
}
}
I don't even understand why am I creating two nested classes, but it seems
like a requirement to create forms in Play? So I made two. This is the
code I use to render the page (it's inside Controller Application)
public static Result index() {
return ok(index.render(form(Login.class), form(Register.class)));
}
OK, now here comes the most frustrated part. First, I don't know if I have
truly authenticated the user when logged in: (codes in Application)
/**
* Handle login form submission.
*/
public static Result authenticate() {
Form<Login> loginForm = form(Login.class).bindFromRequest();
if (loginForm.hasErrors()) {
return badRequest(index.render(loginForm, form(Register.class)));
} else {
session("email", loginForm.get().email);
return redirect(controllers.routes.Wall.index());
}
}
Second, how can I say a user's information when he/she registers. I copied
these lines from the Java for Play book, but it doesn't work (my IDE says
it's wrong..type mismatch)
User user = RegisterForm.get();
I must have done something really really wrong here..can anyone enlighten
me a bit?
No comments:
Post a Comment