Annotated example-form.xml

Author: Anthony Eden

The following configuration is taken from the example application which ships with the FormProc distribution.

Annotation

<?xml version="1.0" encoding="ISO-8859-1"?>

This is required at the begining of every XML document. You can change the encoding value if you need to include foreign characters in your configuration.

<form>

Signifies the start of a form configuration.

	<name>example</name>

The name of the form. This name is used to distinguish this form from other forms.

	<storer classname="org.formproc.store.ReflectionStorer"/>

Define a default storer to be used throughout the form. FormProc supports automatic population of target objects using the ReflectionStorer class. The store system is pluggable. The storer class can be any class which implements the org.formproc.store.Storer interface or extends from the org.formproc.store.AbstractStorer abstract base class. FormProc currently ships with two storers: the ReflectionStorer and the MapStorer which stores values in a map of key/value pairs.

	<element name="name">

The first element definition. Each form element must be defined here.

		<validator type="shared" name="required">
			<error>Name field required.</error>
			<error lang="fr">Le nom valide a exigé.</error>
		</validator>

Define the validator which is used for the FormElement. This particular configuration sets the validator to the shared validator "required" which is defined in the formproc.xml configuration. It also overrides the error messages of the shared validator. Note that error elements can include a lang attribute which specifies the language of the error message. The lang attribute must be a two-character ISO language code as defined in the java.util.Locale class. If no lang attribute is specified then that error message will be used as the default in case an error message for a specific language is unspecified.

		<write-method>setName</write-method>

Specifies the write method used by the reflection storer.

		<message>Required</message>

A message which can be displayed to the user. The message element can include a lang attribute which specifies the message language. The lang attribute requires a two-character ISO language code as defined in the java.util.Locale class. If no lang attribute is specified then that message will be used as the default in case a message for a specific language is unspecified.

	</element>

Close the "name" element's configuration.

	<element name="occupation" optional="true">

Configure a second form element which is similar to the "name" element. This element is for an "occupation" field. The optional="true" attribute makes this element optional. Optional elements will not be validated, converted or stored if their value is null or an empty String.

		<default>Not Defined</default>

Defines the default value which is used if the submitted value is either null or an empty String.

		<message>Optional</message>
	</element>

Specify a message which can be displayed to the user and close the "occupation" form element's configuration.

	<element name="username">
		<validator type="shared" name="username"/>
		<message resource="org.formproc.example.ExampleResourceBundle">username.message</message>
	</element>

This form element definition is uses a shared validator called "username". In addition, it defines a resource bundle and the resource key which is used for determining error messages when validation fails.

	<element name="age" optional="true">
		<validator type="shared" name="age"/>
		<message>Optional.  If specified, a valid age required.</message>
		<message lang="fr">Optional. L'âge valide a exigé.</message>
		<converter classname="org.formproc.conversion.IntegerConverter"/>
	</element>

Optional age field. This form element defines a converter which is used to convert the incoming data. Conversion occurs after validation but prior to storing. FormProc comes with a set of standard converters which can easily be augmented by creating new converter classes which implement the org.formproc.conversion.TypeConverter interface or extend from the org.formproc.conversion.AbstractTypeConverter abstract base class.

	<element name="birthdate">
		<validator type="expression">
			<pattern>\d\d/\d\d/\d\d\d\d</pattern>
			<error>Date must be in format MM/dd/yyyy</error>
		</validator>
		<message lang="en">Valid date (MM/dd/yyyy) required.</message>
		<message lang="fr">Le date valide (MM/dd/yyyy) a exigé.</message>
		<converter classname="org.formproc.conversion.DateConverter">
			<parse type="custom" pattern="MM/dd/yyyy"/>
		</converter>
	</element>

This element definition defines the validator here rather than using a shared validator. It also defines locale-specific messages as well as a custom converter. Notice that converters can have configuration data specified as well. When the converter is created the TypeConverter.loadConfiguration(Configuration configuration) method is called with a configuration tree representing the elements and attributes which are children of the converter element.

	<element name="password">
		<validator type="shared" name="password"/>
		<message classname="org.formproc.message.ResourceBundleMessageProvider">
			<bundle>org.formproc.example.ExampleResourceBundle</bundle>
			<key>password.message</key>
		</message>
	</element>

This element uses a shared validator called "password". It also defines a custom MessageProvider for returning message values. Any class can be used in the message element's classname attribute as long as it implements the org.formproc.message.MessageProvider interface. In this case the custom provider provides the same implementation as the message tag in the "username" element definition.

	<element name="passwordVerify">
		<validator type="shared" name="password"/>
		<message resource="org.formproc.example.ExampleResourceBundle">passwordVerify.message</message>
	</element>

The final element definition is another example of using a resource bundle and resource key to provide localized messages.

	<element-group name="password">
		<element name="password"/>
		<element name="passwordVerify"/>
		<validator type="script">
			<script>FieldsMatch.py</script>
			<error lang="en">Passwords must must match.</error>
			<error lang="fr">La nécessité de mots de passe doit s'assortir.</error>
		</validator>
	</element-group>

This element defines an element group. The element group will execute the specified validator and will provide all of the submitted data for the specified elements. The element group is designed to allow comparison of multiple elements with each other.

</form>

Close the form configuration.