package org.apache.wicket.examples.ajax.builtin;
import org.apache.wicket.ajax.AjaxPreventSubmitBehavior;
import org.apache.wicket.ajax.form.AjaxFormValidatingBehavior;
import org.apache.wicket.feedback.ExactLevelFeedbackMessageFilter;
import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.SimpleFormComponentLabel;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.util.io.IClusterable;
import org.apache.wicket.util.time.Duration;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.apache.wicket.validation.validator.StringValidator;
@author
public class FormPage extends BasePage
{
public FormPage()
{
final FeedbackPanel feedbackErrors = new FeedbackPanel("feedbackErrors", new ExactLevelFeedbackMessageFilter(FeedbackMessage.ERROR));
feedbackErrors.setOutputMarkupId(true);
add(feedbackErrors);
final FeedbackPanel feedbackSuccess = new FeedbackPanel("feedbackSuccess", new ExactLevelFeedbackMessageFilter(FeedbackMessage.INFO));
feedbackSuccess.setOutputMarkupId(true);
add(feedbackSuccess);
addInstantValidationForm();
addPreventEnterSubmitForm();
}
private void addPreventEnterSubmitForm() {
Bean bean = new Bean();
Form<Bean> form = new Form<Bean>("preventEnterForm", new CompoundPropertyModel<>(bean))
{
@Override
protected void onSubmit()
{
super.onSubmit();
info("Form successfully submitted!");
}
};
add(form);
form.setOutputMarkupId(true);
addFormComponents(form);
form.add(new AjaxPreventSubmitBehavior());
}
private void addInstantValidationForm() {
Bean bean = new Bean();
Form<Bean> form = new Form<Bean>("form", new CompoundPropertyModel<>(bean))
{
@Override
protected void onSubmit()
{
super.onSubmit();
info("Form successfully submitted!");
}
};
add(form);
form.setOutputMarkupId(true);
addFormComponents(form);
form.add(new AjaxFormValidatingBehavior("keydown", Duration.ONE_SECOND));
}
private void addFormComponents(final Form<Bean> form) {
FormComponent<String> fc = new RequiredTextField<>("name");
fc.add(new StringValidator(4, null));
fc.setLabel(new ResourceModel("label.name"));
form.add(fc);
form.add(new SimpleFormComponentLabel("name-label", fc));
fc = new RequiredTextField<>("email");
fc.add(EmailAddressValidator.getInstance());
fc.setLabel(new ResourceModel("label.email"));
form.add(fc);
form.add(new SimpleFormComponentLabel("email-label", fc));
}
public static class Bean implements IClusterable
{
private String name, email;
@return
public String getEmail()
{
return email;
}
@param
public void setEmail(String email)
{
this.email = email;
}
@return
public String getName()
{
return name;
}
@param
public void setName(String name)
{
this.name = name;
}
}
}