Writing Custom Validators in JSF 1.2 with Facelets 1.1.12

I looked at the following link and found out that the javax.faces.webapp.ValidatorTag

ValidatorELTag is deprecated and replaced, however I can't seem to find good information on this.

I want to create a Regular Expression Regulator that takes input: a regular expression and an error message.

Meaning I would like to have a control like this:

<regexValidator for="myControl" check="([a-Z]^)" errorMessage="Your input contained incorrect characters" />

      

Now the given link above shows how to do a small part of this, but a lot has changed since this was written and the methods are outdated, how do I approach this issue?

+2


source to share


2 answers


See Creating a Custom Validator in the JEE5 Tutorial . The section on Creating a custom tag describes how to implement the class ValidatorELTag

.


for="myControl"

      

I doubt you will need this attribute (I'm not sure how you use it). The validator will be installed on the parent control. for

attributes are usually only used when one control is referencing another, as in a label component.


EDIT: I misunderstood the question; the above answer is specific to JSP (those classes related to tags are mostly JSF 1.2 are for JSP only; Facelets has its own tagging system ; the good news is you don't need a Java class specifically to define a tag).

Plausibility check example:



public class RegexValidator implements Validator, StateHolder {
  private boolean isTransient;
  private String regex;

  public String getRegex() { return regex; }
  public void setRegex(String regex) { this.regex = regex; }

  public void validate(FacesContext context, UIComponent component, Object value)
      throws ValidatorException {
    //TODO: throw ValidatorException if not valid
  }

  //TODO: implement remaining StateHolder methods...
}

      

This validator is then registered with faces-config.xml

:

  <validator>
    <validator-id>regex.validator</validator-id>
    <validator-class>val.RegexValidator</validator-class>
  </validator>

      

Then you add the tag library to your application (for example WEB-INF/facelets/foo.taglib.xml

):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE facelet-taglib PUBLIC
  "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
  "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib xmlns="http://java.sun.com/JSF/Facelet">
  <namespace>http://demo</namespace>
  <tag>
    <tag-name>regexValidator</tag-name>
    <validator>
      <validator-id>regex.validator</validator-id>
    </validator>
  </tag>
</facelet-taglib>

      

Add an ad xmlns:demo="http://demo"

to any Facelets you want to use the tag library in your validation tag will start on the form <demo:regexValidator ...

; attributes will be obtained by introspection of the validator class.

+3


source


Here is the part I forgot to add to my code that spurred me on ...

<context-param>
    <param-name>facelets.LIBRARIES</param-name>
    <param-value>/WEB-INF/faces/foo.taglib.xml</param-value>
</context-param>

      



You will also want to change check="([a-Z]^)"

toregex="..."

I like this approach because there is no need to extend ValidatorELTag

. I'm a very big fan of chips and this is another great feature.

0


source







All Articles