Example usage for com.google.gwt.libideas.validation.client ValidatorController addAsFocusListener

List of usage examples for com.google.gwt.libideas.validation.client ValidatorController addAsFocusListener

Introduction

In this page you can find the example usage for com.google.gwt.libideas.validation.client ValidatorController addAsFocusListener.

Prototype

public static ValidatorController addAsFocusListener(TextBox box, Validator validator) 

Source Link

Document

Creates a new ValidatorController for the given text box, which is checked when the text box losses focus.

Usage

From source file:com.google.gwt.demos.validation.client.ValidationDemo.java

License:Apache License

/**
 * Create the sample booking form.// w  w  w  .  j  av a 2  s.  c  om
 */
private void createBookingForm() {

    // /////////////////////////////////////////////
    // World's most boring ui for a form.
    // ////////////////////////////////////////////
    booking = new FlexTable();
    booking.getColumnFormatter().setStyleName(0, "bookingLabel");
    RootPanel.get().add(booking);

    addTextField("Name");
    TextBox birth = addTextField("Birth");
    final TextBox crimeCode = addTextField("Crime Code");
    TextBox dateOfCrime = addTextField("Date of Crime");
    addTextField("Arresting Officer");
    TextBox phone = addTextField("Phone Number");

    // /////////////////////////////////////////////
    // Validation Support.
    // ////////////////////////////////////////////

    // Cannot book anyone younger than 18 or older than 100.
    Validator crimeValidator = new DateRangeValidator(offsetDate(100), offsetDate(18));
    ValidatorController crimeValidation = ValidatorController.addAsFocusListener(birth, crimeValidator);

    // Crime codes must be correctly formatted.
    ValidatorController.addAsFocusListener(crimeCode, crimeCodeFormatValidator);

    // Phone numbers can be pushed into shape if they are almost correctly
    // formatted.
    ValidatorController.addAsFocusListener(phone, new USPhoneValidator());

    // Creates a custom validator.
    Validator statuteValidator = createStatuteValidator(crimeCode);

    // Can add the same validator to multiple subjects to do multi-field
    // validation.
    crimeValidation.addValidator(statuteValidator);
    ValidatorController.addAsFocusListener(dateOfCrime, statuteValidator);

    // Cannot book someone for a crime more than 30 years old.
    ValidatorController.addAsFocusListener(dateOfCrime, new DateRangeValidator(offsetDate(30), new Date()));
}