org.apache.tapestry.corelib.components.BeanEditForm

A component that creates an entire form editing the properties of a particular bean. Inspired by Trails and BeanForm (both for Tapestry 4). Generates a simple UI for editing the properties of a JavaBean, with the flavor of UI for each property (text field, checkbox, drop down list) determined from the property type, and the order and validation for the properties determined from annotations on the property's getter and setter methods. You may add t:parameters to the component; when the name matches (case insensitive) the name of a property, then the corresponding Block is renderered, rather than any of the built in property editor blocks. This allows you to override specific properties with your own customized UI, for cases where the default UI is insufficient, or no built-in editor type is appropriate.

[JavaDoc]

Component Parameters

NameTypeFlagsDefaultDefault PrefixDescription
clientValidationbooleanpropIf true, the default, then the embedded Form component will use client-side validation.
excludeStringliteralA comma-separated list of property names to be removed from the org.apache.tapestry.beaneditor.BeanModel. The names are case-insensitive.
includeStringliteralA comma-separated list of property names to be retained from the org.apache.tapestry.beaneditor.BeanModel. Only these properties will be retained, and the properties will also be reordered. The names are case-insensitive.
modelorg.apache.tapestry.beaneditor.BeanModelpropThe model that identifies the parameters to be edited, their order, and every other aspect. If not specified, a default bean model will be created from the type of the object bound to the object parameter.
objectObjectRequiredpropThe object to be edited. This will be read when the component renders and updated when the form for the component is submitted. Typically, the container will listen for a "prepare" event, in order to ensure that a non-null value is ready to be read or updated. Often, the BeanEditForm can create the object as needed (assuming a public, no arguments constructor). The object property defaults to a property with the same name as the component id.
reorderStringliteralA comma-separated list of property names indicating the order in which the properties should be presented. The names are case insensitive. Any properties not indicated in the list will be appended to the end of the display order.
submitLabelStringmessage:submit-labelliteralThe text label for the submit button of the form, by default "Create/Update".

The BeanEditForm component is a convienent wrapper around three components: Form, Errors and BeanEditor.

Related Components

Simple Example

Using the bean editor, we can easily create a simple form for collecting information from the user. In this example, we'll collect a little bit of data about a User:

The bean to edit will be a property of the containing page.

User.java

public class User
{
    private long _id;

    private String _firstName;

    private String _lastName;

    private int _age;

    public long getId() { return _id; }

    @NonVisual
    public void setId(long id) { _id = id; }

    public String getFirstName() { return _firstName; }

    public void setFirstName(String firstName) { _firstName = firstName; }

    public String getLastName() { return _lastName; }

    public void setLastName(String lastName) { _lastName = lastName; }

    public int getAge() { return _age; }

    public void setAge(int age) { _age = age; }
}

The @NonVisual annotation prevents the id from being displayed.

CreateUser.tml

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
    <body>
        <h1>Create New User</h1>

        <t:beaneditform t:id="user" submitlabel="message:create-user"/>
    </body>
</html>

Nominally, we should have to bind the object parameter of the BeanEditForm component. However, as a convienience, Tapestry has defaulted the object parameter based on the component id. This works because the CreateUser class includes a property named "user", which matches the BeanEditForm component's id.

When the object to be editted is not a direct property of the page, it will be necessary to bind the object parameter explicitly. For example, object="registration.address" to create or edit the address property of the page's registration property. Component ids may not contain periods, so there's no way to specify this without the object parameter. A best practice is to still explicitly set the component's id, thus: <t:beaneditform t:id="address" object="registration.address"/>

CreateUser.properties

create-user=Create New User
firstname-label=Given Name
lastname-label=Family Name

We are using the page's message catalog to supply a messages. Externalizing such messages make them easier to work with, especially for an application that may be localized.

The create-user key is explicitly referenced (submitlabel="message:create-user"). This becomes the label on the submit button for the generated form.

The two label keys will be picked up and used as the labels for the corresponding properties (in both the rendered <label> elements, and in any error messages).

In many cases, common entries can be moved up to an application-wide message catalog. In that case, the page's own message catalog becomes a local override.

CreateUser.java

public class CreateUser
{
    @Persist
    private User _user;

    @Inject
    private UserDAO _userDAO;

    public User getUser()
    {
      return _user;
    }

    public void setUser(User user)
    {
      _user = user;
    }

    Object onSuccess()
    {
        _userDAO.add(_user);

        return UserAdmin.class;
    }
}

Notice that we don't instantiate the User object ourselves; instead we let the BeanEditForm component do that for us. It's capable of doing so because the User class has a default public no arguments constructor.

The onSuccess() method is invoked when the form is submitted with no validation errors (although client validation is enabled by default, server-side validation is always enforced). The UserDAO service is used to add the new user.

Returning a class from an event handler method (UserAdmin.class) will activate the indicated page as the response page. As always, a redirect to to the response page is sent to the client.

Validations and Overrides

By placing some annotations on the properties of the User class, we can enable client-side validations. In addition, we can override the default editor components for a property to add some additional instructions.

User.java (partial)

    @Validate("required")
    public String getFirstName() { return _firstName; }

    public void setFirstName(String firstName) { _firstName = firstName; }

    @Validate("required")
    public String getLastName() { return _lastName; }

    public void setLastName(String lastName) { _lastName = lastName; }

    @Validate("min=18,max=99")
    public int getAge() { return _age; }

    public void setAge(int age) { _age = age; }    

The new @Validate annotations added to the first name and last name properties indicates that a non-blank value must be provided. For the age property we are setting minimum and maximum values as well.

Validation for each field occurs when the form is submitted, and when the user tabs out of a field. If you submit immediately, Tapestry will display popup bubbles for each field identifying the error:

In addition, fields with errors are marked with a red X, the font for the first turns red, and the label for the field turns red. We're providing a lot of feedback to the user. After a moment, all the bubbles except for the current field fade. Bubbles fade in and out as you tab from field to field.

CreateUser.tml (partial)

We can customize how individual properties are editted. Here we'll add a small reminder next to the age property:

        <t:beaneditform t:id="user" submitlabel="message:create-user">
            <t:parameter name="age">
                <t:label for="age"/>
                <t:textfield t:id="age" value="user.age"/>
                <em>
                    Users must be between 18 and 99.
                </em>
            </t:parameter>
        </t:beaneditform>

The <t:parameter> element is an override for the property. The name is matched against a property of the bean. We need to provide a Label component, and an appropriate editor component.

Notes

You can re-order the properties using the reorder parameter:

<t:beaneditform t:id="user" reorder="lastname,firstname"/>

You can accomplish the same thing by changing the order of the getter methods in the bean class. The default order for properties is not alphabetical, it is the order of the getter methods.

You can also remove properties with the exclude parameter, which is equivalent to the @NonVisual annotation.


Back to index