简体   繁体   中英

Play Framework Java - Form validation error

I'm trying to fill my User Form in Java Play! 2.4.3, but i always get a "IllegalStateException: No value"

This is the code from the Controller:

Form<User> uf = Form.form(User.class);

uf.fill( new User( "felix@abc.com" , "123") );

if (uf.hasErrors()){
    return ok("Form Error");
}

// IllegalStateException: No value
uf.get();

User.class:

@Constraints.Required
private String email;
@Constraints.Required
private String password;

public User(){}

public User(String email, String password) {
    this.email = email;
    this.password = password;
}

//Getter
public String getEmail() {
    return email;
}

public String getPassword() {
    return password;
}

//Setter
public void setEmail(String email) {
    this.email = email;
}

public void setPassword(String password) {
    this.password = password;
}

I also tried to fill the Form with the bind Method and a HashMap but get the same Error

Form<User> uf = Form.form(User.class);    

Map<String,String> data = new HashMap();
data.put("email", "felix@abc.com");
data.put("password", "123");

User user = uf.bind(data);

if (uf.hasErrors()){
    return ok("Form Error");
}

// IllegalStateException: No value
uf.get();

The .fill method returns a new Form object ( see here ). So I belive this should work (didnt test):

Form<User> uf = Form.form(User.class);

Form<User> newUf = uf.fill( new User( "felix@abc.com" , "123") );

if (newUf.hasErrors()){
    return ok("Form Error");
}

// Should Work
newu=Uf.get();

And it seems the story is also the same for .bind ( as seen here ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM