Panayiotis Karabassis
Panayiotis Karabassis

Reputation: 2288

Custom validator with attributes in facelets

I have written a custom Validator that expects an attribute to be set. I know I have to write a Handler (TagHandler or ValidatorHandler?) to set that attribute, but I am having trouble doing it.

<tag>
    <tag-name>validateCustom</tag-name>
    <validator>
        <validator-id>package.CUSTOM_VALIDATOR</validator-id>
    </validator>
</tag>

Where do i put the handler-class element? And how do I write the handler so that it will pass the attributes to my custom validator?

Thanks in advance!

Upvotes: 2

Views: 2598

Answers (2)

Paul Wasilewski
Paul Wasilewski

Reputation: 10372

The suggested solution doesn't work and it's overcomplicated as well. There is no need of a handler class in JSF 1.2 and there is no support for a handler class in the validator tag at all.

So all you need is.

The Validator:

public class CustomValidator implements Validator, Serializable
{
    private static final long serialVersionUID = ...;
    private String someAttribute;

    public void setSomeAttribute(String someAttribute)
    {
        this.someAttribute = someAttribute;
    }

    @Override
    public void validate(FacesContext context, UIComponent component, Object value)
    throws ValidatorException 
    {
     // Do validation
    } 
}

The declaration/registration of the custom validator in the faces-config.xml

<validator>
    <validator-id>customValidator</validator-id>
    <validator-class>package.CustomValidator</validator-class>
</validator>

The taglib.xml (e.g. my.taglib.xml):

<?xml version="1.0"?>
<!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>
<namespace>http://myNS.com/jsf</namespace>
<tag>
    <tag-name>myCustomValidator</tag-name>
    <validator>
        <validator-id>customValidator</validator-id>
    </validator>
</tag>
</facelet-taglib>

Finally the registration of the taglib.xml in web.xml:

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

So thats all no need of a handler :-)

Upvotes: 0

Panayiotis Karabassis
Panayiotis Karabassis

Reputation: 2288

Here is a validator with custom attribute skeleton:

public class CustomValidator implements Validator, Serializable
{
    private static final long serialVersionUID = ...;
    private String someAttribute;

    public void setSomeAttribute(String someAttribute)
    {
        this.someAttribute = someAttribute;
    }

    @Override
    public void validate(FacesContext context, UIComponent component, Object value)
    throws ValidatorException 
    {
        // Do validation
    }
}

Here is the tag handler:

public class CustomValidatorHandler extends ValidateHandler
{
    private final TagAttribute someAttribute;

    public CustomValidatorHandler(ValidatorConfig config)
    {
        super(config);

        someAttribute = this.getAttribute("someAttribute");
    }

    @Override
    protected Validator createValidator(FaceletContext ctx)
    {
        CustomValidator result = (CustomValidator) ctx
        .getFacesContext()
        .getApplication()
        .createValidator("package.CUSTOM_VALIDATOR");

        if (someAttribute != null) result.setSomeAttribute(someAttribute.getValue(ctx));

        return result;
    }
}

And here is part of the taglib file:

<tag>
    <tag-name>validateCustom</tag-name>
    <validator>
        <validator-id>package.CUSTOM_VALIDATOR</validator-id>
    <handler-class>package.CustomValidatorHandler</handler-class>
    </validator>
</tag>

Upvotes: 4

Related Questions