![]() If you implemented a Validator interface rather than implementing a managed bean method that performs the validation, you need to do one of the following.Īllow the page author to specify the Validator implementation to use with the f:validator tag. See Saving and Restoring State for more information. ![]() To implement StateHolder, you would need to implement its four methods: saveState(FacesContext), restoreState(FacesContext, Object), isTransient, and setTransient(boolean). To do so, you will need to implement the StateHolder interface as well as the Validator interface. You may wish to save and restore state for your validator, although state saving is not usually necessary. The setter method calls the parseFormatPatterns method, which separates the components of the pattern string into a string array, formatPatternsList. The hypothetical FormatValidator class also defines accessor methods for setting the formatPatterns attribute, which specifies the acceptable format patterns for input into the fields. However, the rest of this section describes how this validator would be implemented and how to specify a custom tag so that the page author could register the validator on a component.ġ5.11.1 Implementing the Validator InterfaceĪ Validator implementation must contain a constructor, a set of accessor methods for any attributes on the tag, and a validate method, which overrides the validate method of the Validator interface. The f:validateRegex tag makes a custom validator unnecessary in this situation. Here is the custom validator tag: Īccording to this validator, the data entered in the field must be one of the following:Ī 16-digit number with a space between every four digitsĪ 16-digit number with hyphens between every four digits This validator would be used with a Credit Card Number field on a Facelets page. You can queue the error messages onto the FacesContext programmatically, or you can register the error messages in the application configuration resource file, as explained in Registering Application Messages.įor example, an e-commerce application might use a general-purpose custom validator called FormatValidator.java to validate input data against a format pattern that is specified in the custom validator tag. You need to store these error messages in a resource bundle.Īfter creating the resource bundle, you have two ways to make the messages available to the application. Usually, you will want to display an error message when data fails validation. You can use the f:validator tag's binding attribute to bind the Validator implementation to the managed bean property. You can also create a managed bean property that accepts and returns the Validator implementation you create, as described in Writing Properties Bound to Converters, Listeners, or Validators. If you prefer to configure the attributes in the Validator implementation, you can forgo specifying a custom tag and instead let the page author register the validator on a component using the f:validator tag, as described in Using a Custom Validator. If you choose to implement the Validator interface and you want to allow the page author to configure the validator's attributes from the page, you also must specify a custom tag for registering the validator on a component. ![]() The rest of this section explains how to implement the Validator interface. ![]() Writing a Method to Perform Validation explains how to implement a managed bean method to perform validation. Provide an implementation of the interface to perform the validation. Implement a managed bean method that performs the validation. As explained in Validation Model, there are two ways to implement validation code. If the standard validators or Bean Validation don't perform the validation checking you need, you can create a custom validator to validate user input. 15.11 Creating and Using a Custom Validator ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |