import wicket.markup.html.WebPage;
import wicket.markup.html.panel.FeedbackPanel;
import wicket.markup.html.panel.ComponentFeedbackPanel;
import wicket.markup.html.form.Form;
import wicket.markup.html.form.TextField;
import wicket.markup.html.form.RequiredTextField;
import wicket.markup.html.form.validation.RequiredValidator;
import wicket.model.CompoundPropertyModel;

import java.io.Serializable;

public class FormValidatePage extends WebPage {
	/**
	 * Constructor. Having this constructor public means that you page is
	 * 'bookmarkable' and hence can be called/ created from anywhere.
	 */
	public FormValidatePage() {
		Person p = new Person("Corey", "3");

		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		add(feedback);

		PersonForm personForm = new PersonForm("person", p, this);
		add(personForm);
	}

	class PersonForm extends Form {

		/**
		 * @param id	 See Component
		 * @param person See Person
		 * @see wicket.Component#Component(String, wicket.model.IModel)
		 */
		public PersonForm(String id, Person person, FormValidatePage formValidatePage) {
			super(id, new CompoundPropertyModel(person));
			TextField name = new TextField("name");
			name.add(RequiredValidator.getInstance());
			add(name);
			add(new ComponentFeedbackPanel("nameError", name));

			TextField strengthTextField = null;
			add(strengthTextField = new RequiredTextField("strength"));
			add(new ComponentFeedbackPanel("strengthError", strengthTextField));
		}
	}

	static class Person implements Serializable {
		String name;
		String strength;

		public Person(String name, String strength) {
			this.name = name;
			this.strength = strength;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getStrength() {
			return strength;
		}

		public void setStrength(String strength) {
			this.strength = strength;
		}

		public String toString() {
			final StringBuffer sb = new StringBuffer();
			sb.append("Person");
			sb.append("{name='").append(name).append('\'');
			sb.append(", strength='").append(strength).append('\'');
			sb.append('}');
			return sb.toString();
		}
	}
}
