form.add(ageField);
form.add(dateField);
form.add(save);
form.add(cancel);
save.addBehavior(new DefaultAjaxBehavior() {
@Override
public ActionResult onAction(Control source) {
// Update the form which might contain errors
return new ActionResult(form.toString(), ActionResult.HTML);
}
});
cancel.addBehavior(new DefaultAjaxBehavior() {
@Override
public ActionResult onAction(Control source) {
// Update the form and ensure errors and values have been cleared
form.clearValues();
form.clearErrors();
return new ActionResult(form.toString(), ActionResult.HTML);
}
});
// NOTE: we add a Behavior to Form so that Click registers the Form as an Ajax target
// ALSO NOTE: we don't implement the onAction method as the save and cancel Submits
// handles the Behavior action event
form.addBehavior(new DefaultAjaxBehavior());
// Instead of adding a behavior, the same can be achived by explicitly registering the Form as an Ajax Target:
// ControlRegistry.registerAjaxTarget(form);
}