I try to make form for user to register using Wicket. I got user POJO and wicket form - this needs to have "repeat password" field which should be in no way connected to User object. But how can I do this? I
public class RegisterForm extends Form<User> {
private static final long serialVersionUID = -9071906666130179515L;
public RegisterForm(String id) {
super(id, new CompoundPropertyModel<User>(new User()));
PasswordTextField pass = new PasswordTextField("password");
pass.setType(String.class);
PasswordTextField pass2 = new PasswordTextField("password2");
pass2.setType(String.class);
pass2.setDefaultModelObject("");
add(new EqualPasswordInputValidator(pass, pass2));
开发者_开发知识库 add(new TextField<String>("login")
.setType(String.class)
.setRequired(true)
.add(new PatternValidator("[a-z0-9]*")));
add(new TextField<String>("email")
.setType(String.class)
.add(EmailAddressValidator.getInstance()));
add(pass);
add(pass2);
}
But i get
java.lang.IllegalStateException: Attempt to set model object on null model of component:
or that User model has no password2 related methods. How to handle such a case?
This should do it:
PasswordTextField pass2 = new PasswordTextField("password2", Model.of(""));
Explanation: CompoundPropertyModel
associates nested form elements with the parent model (component name foo
is mapped to the bean.foo
property of the parent model). You can overwrite this behavior by assigning a different model to the child component.
I would have used a property in the form and a PropertyModel
. This way I would have access to the field throught the getPassword2()
method.
public class RegisterForm extends Form<User> {
private static final long serialVersionUID = -9071906666130179515L;
// password2 Property
protected String password2 = "";
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
// end password2 Property
public RegisterForm(String id) {
super(id, new CompoundPropertyModel<User>(new User()));
PasswordTextField pass = new PasswordTextField("password");
pass.setType(String.class);
// add new PropertyModel
PasswordTextField pass2 = new PasswordTextField("password2", new PropertyModel<String>(this, "password2"));
add(new EqualPasswordInputValidator(pass, pass2));
add(new TextField<String>("login")
.setType(String.class)
.setRequired(true)
.add(new PatternValidator("[a-z0-9]*")));
add(new TextField<String>("email")
.setType(String.class)
.add(EmailAddressValidator.getInstance()));
add(pass);
add(pass2);
}
精彩评论