Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: More 5.3 updates

...

Code Block
xml
xml
titleCreateAddress.tml (partial)
  <t:beaneditform object="address"
    reorder="honorific,firstName,lastName,street1,street2,city,state,zip,email,phone" />

Ultimately, however we will be using the BeanEditForm with the Address entity on several pages, and don't want to have to reiterate that list of properties every time.

Customizing labels

Tapestry makes it pretty easy to customize the labels used on the fields. It's just a matter of creating a message catalog for the page.

...

Code Block
XML
XML
  <t:beaneditform object="address" submitlabel="message:submit-label"
    object="address"/>reorder="honorific,firstName,lastName,street1,street2,city,state,zip,email,phone" />

And then define And then define the submit-label key in the message catalog:

No Format
submit-label=Create Address

At then end of the day, the exact same HTML is sent to the client, regardless of whether you include the label text directly in the template, or indirectly in the message catalog. In the long term, the latter approach will work better if you later chose to internationalize your application.

...

The BeanEditForm checks for a Tapestry-specific annotation, @org.apache.tapestry5.beaneditor.Validate, on the getter or setter method of each property.

Update the getter methods for the lastName, firstName, street1, city, state and zip fields, adding a @Validate annotation to each:

Code Block
  @Validate("required")
  public String getFirstName()
  {
    return firstName;
  }

What is that string, "required"? That's how you specify the desired validation. It is a series of names that identify what type of validation is desired. A number of validators are built in, such as "required", "minLength" and "maxLength". As elsewhere, Tapestry is case insensitive.

You can apply multiple validations, by separating the validator names with commas. Some validators can be configured (with an equals sign). Thus you might say "required,minLength=5" for a field that must be specified, and must be at least five characters longspecified, and must be at least five characters long.

Warning

You can easily get confused when you make a change to an entity class, such as adding the @Validate annotatation, and not see the result in the browser. Only component classes, and (most) classes in the Tapestry services layer, are live-reloaded. Data and entity objects are not reloaded, so this is one area where you need to stop and restart Jetty to see the change.

Restart the application, and refresh your browser, then hit the submit button.

...

Code Block
  @Validate("required,regexp=^\\d{5}(-\\d{4})?$")
  public String getZip()
  {
    return zip;
  }

Let's give it a try; restart the application and enter an "abc" for the zip code.

...

No Format
zip-regexp-message=Zip Codes are five or nine digits.  Example: 02134 or 90125-1655.

Refresh the page and submit again:

...

Code Block
  @Validate("required,regexp")
  public String getZip()
  {
    return zip;
  }

Now, just put the regular expression into the CreateAddress message catalog:

No Format
zip-regexp=^\\d{5}(-\\d{4})?$
zip-regexp-message=Zip Codes are five or nine digits.  Example: 02134 or 90125-1655.

After a restart you'll see the ... the same behavior. But when we start creating more complicated regular expressions, it'll be much, much nicer to put them in the message catalog rather than inside the annotation value. And inside the message catalog, you can change and tweak the regular expressions without having to restart the application each time.

...