
Standard JSR 303 defines a set of annotations and APIs to validate our domain objects at field-level. Wicket has introduced an experimental support for this standard since version 6.4.0 and with version 6.14.0 it has became an official Wicket module (named _wicket-bean-validation_).
In this paragraph we will see the basic steps needed to use JSR 303 validation in our Wicket application. Code snippets are from example project _JSR303validation_.

In the example application we have a form to insert the data for a new _Person_ bean and its relative _Address_. The code for class _Person_ is the following

[source,java]
----
public class Person implements Serializable{

	@NotNull
	private String name;

	//regular expression to validate an email address
	@Pattern(regexp = "^[_A-Za-z0-9-]+(.[_A-Za-z0-9-]+)*[A-Za-z0-9-]+(.[A-Za-z0-9-]+)*((.[A-Za-z]{2,}){1}$)")
	private String email;

	@Range(min = 18, max = 150)
	private int age;

	@Past @NotNull
	private Date birthDay;

	@NotNull
	private Address address;
}
----

You can note the JSR 303 annotations used in the code above to declare validation constraints on class fields. Class _Address_ has the following code:

[source,java]
----
public class Address implements Serializable {

	@NotNull
	private String city;

	@NotNull
	private String street;

	@Pattern(regexp = "\\d+", message = "{address.invalidZipCode}")
	private String zipCode;
}
----

You might have noted that in class _Address_ we have used annotation _Pattern_ using also attribute _message_ which contains the key of the bundle to use for validation message. Our custom bundle is contained inside _HomePage.properties_:

[source,java]
----
address.invalidZipCode=The inserted zip code is not valid.
----

To tell Wicket to use JSR 303, we must register bean validator on Application's startup:

[source,java]
----
public class WicketApplication extends WebApplication {
	@Override
	public void init(){
		super.init();

		new BeanValidationConfiguration().configure(this);
	}
}
----

The last step to harness JSR 303 annotations is to add validator _org.apache.wicket.bean.validation.PropertyValidator_ to our corresponding form components:

[source,java]
----
public HomePage(final PageParameters parameters) {
	super(parameters);

	setDefaultModel(new CompoundPropertyModel<Person>(new Person()));

	Form<Void> form = new Form<Void>("form");

	form.add(new TextField("name").add(new PropertyValidator()));
	form.add(new TextField("email").add(new PropertyValidator()));
	form.add(new TextField("age").add(new PropertyValidator()));
        //...
}
----

Now we can run our application an see that JSR 303 annotations are fully effective:

image::./img/jsr303-form-validation.png[]
