How to dynamically change required to false
I'm new to Wicket. Can someone please tell me how to dynamically set required
to false
? Here is my code:
AjaxButton cance开发者_如何学Gol=new AjaxButton("cancel"){
public void onSubmit(AjaxRequestTarget target, Form form){
passwrd.setRequired(false);
nameField.setRequired(false);
usernameField.setRequired(false);
LecturerPage lecturer=new LecturerPage();
setResponsePage(lecturer);
}
};
addstud.add(cancel);
Tomcat is telling me that my feedback panel is being left unrendered (my OK button makes use of a feedback panel).
The onSubmit() method is executed only after all validation is done, so this code isn't even being executed.
But, you often need some actions to be executed without any validation (like your 'cancel' button). You can do that calling
cancel.setDefaultFormProcessing(false);
This way, Wicket will ignore validation and form updating, and will execute the onSubmit() method right away.
Then, your code would look like this:
AjaxButton cancel = new AjaxButton("cancel") {
public void onSubmit(AjaxRequestTarget target, Form form){
setResponsePage(new LecturerPage());
}
};
cancel.setDefaultFormProcessing(false);
Wicket's complain about the feedback not being rendered happens because, since you haven't called setDefaultFormProcessing(false), it will process the form, try to validate its fields, and find errors. And, at the end of the request cycle, the error messages won't have been displayed anywhere, since you didn't request the FeedbackPanel to be re-rendered. To display validation errors on ajax forms, you must override the AjaxButton.onError() method:
AjaxButton save = new AjaxButton("save") {
public void onSubmit(AjaxRequestTarget target, Form form) {
// whatever. won't run if any validation fails
}
public void onError(AjaxRequestTarget target, Form<?> form) {
target.addComponent(feedbackPanel);
}
};
精彩评论