in Advanced 

Question preview

HomeQuestion preview:
Log in

JSF Validator in Jboss Seam. How do I pass param?

Hi,
Frameworks: Jboss SEAM + JSF + Facelets.
Is there a way to pass a parameter to a validator which is also a SEAM component?
Validator is not present in faces-config.xml

The validator is marked with SEAM annotation in order not to be placed in faces-config.xml.

@org.jboss.seam.annotations.faces.Validator(id = "myValidator")
@Name("myValidator")
public class myValidator implements Validator, Serializable {
    private int min;
.......................................
}

The question is how do I fill in the "min" param from JSF page?

At the moment I call the validator like this but the "min" param is not filled in.
<h:inputText id="someName" value="#{bean.name}" required="true">
<f:validator validatorId="myValidator" >
     <f:param id="min" value="2"/>
</f:validator>
</h:inputText>
Do you know someone who could answer? Ask him for help
Answers: 1
Sort by: date rating
image xpert
10:41/07.01.2010
5 from possible 5 with 1 votes
Hi,
First of all you need to make custom facelete tag.
Make taglib called myvalidators.taglib.xml and place it in classpath under path
/META-INF/myvalidators.taglib.xml with the followig content:
<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "facelet-taglib_1_0.dtd">
<facelet-taglib>
    <namespace>http://example.com/jsf/my/validators</namespace>
    <tag>
        <tag-name>myValidator</tag-name>
        <validator>
            <validator-id>myValidator</validator-id>
        </validator>
    </tag>
</facelet-taglib>


Important thing is to place "myvalidators.taglib.xml" in /META-INF/ directory in some of your jars from class path in order facelets to autodiscover it. This is "well known location" for facelet taglibs. Also important thing is the suffix of file name ".taglib.xml".

Then write your validator and mark it with seam annotations. Write your code that validates the edit field. You now can use your example property as it is already set automatically.

package com.example.validators;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.ScopeType;

import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import javax.faces.validator.ValidatorException;
import javax.faces.application.FacesMessage;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.faces.component.UIComponent;
import java.io.Serializable;

@org.jboss.seam.annotations.faces.Validator(id = "myValidator")
@Name("myValidator")
public class MyCustomValidator implements Validator, Serializable {
    private int exampleProp;

    public void validate(FacesContext facesContext, UIComponent c, Object o) throws ValidatorException {
        if (...condition...) {
            throw new ValidatorException(new FacesMessage(...whatever...  ));
        }
    }
    public int getExampleProp() {
        return exampleProp;
    }

    public void setExampleProp(int exampleProp) {
        this.exampleProp = exampleProp;
    }



Annotating the validator with seam annotations saves your declaration in faces-config.xml . You properties from xhtml page are autowired to your validator's properties.

In XHTML you can use your validator like this:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:s="http://jboss.com/products/seam/taglib"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:a4j="http://richfaces.org/a4j"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:my="http://example.com/jsf/my/validators">

                <h:inputText id="someField" value="#{somebean.name}" 
                                   required="true">
                    <my:myValidator exampleProp="100"/>
                </h:inputText>

</ui:composition>

Vote:
Please vote! Your opinion matters!
If you haven't found what you've looking for, post a question
Ask question
| Home | Hall of fame | Registration | Log in | Terms of service | Privacy policy | Help | Contacts | RSS |