0

I have a custom converter which is this :

@ManagedBean
@RequestScoped
public class MeasureConverter implements Converter {

    @EJB
    private EaoMeasure eaoMeasure;

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (!(value instanceof Measure) || ((Measure) value).getId() == null) {
            return null;
        }

        return String.valueOf(((Measure) value).getId());
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if (value == null || !value.matches("\\d+")) {
            return null;
        }

        Measure measure = eaoMeasure.find(Integer.valueOf(value));

        if (measure == null) {
            throw new ConverterException(new FacesMessage("Unknown Measure ID: " + value));
        }

        return measure;
    }

But I would like to validate this as I'm doing with the other fields, just like this for example :

<h:outputLabel for="name" value="Name:" />
<h:inputText id="name" value="#{bean.name}">
<f:ajax event="blur" listener="#{validator.name}" render="m_name" />
</h:inputText>
<rich:message id="m_name" for="name" ajaxRendered="false" />

So when the blur event occurs an icon appears indicating if the selection in correct or not (the code above do this what I'm talking )

But I want to apply this kind of validation in this same custom converter, just to keep the standard validation through my project to the all kinds of inputs or sorts of.

I already try to use a @ManagaProperty in a RequestScope but without success:

@ManagedProperty("#{param.measure}")
private String measure;

So when the blur events occurs it calls my validate bean which has the requestScope as BalusC said here, but what it comes is a empty string.

Any idea ?

Community
  • 1
  • 1
Valter Silva
  • 16,446
  • 52
  • 137
  • 218

1 Answers1

1

You should implement a Validator.

E.g.

@FacesValidator("measureValidator")
public class MeasureValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        Measure measure = (Measure) value;

        // ...

        if (!valid) { 
            throw new ValidatorException(new FacesMessage("Not valid"));
        }
    }

}

You can declare it by the input component's validator attribute

<h:inputText validator="measureValidator">

or by the <f:validator> tag (yes, you can have multiple of them; they will be invoked in the order they are been attached to the component)

<h:inputText>
    <f:validator validatorId="measureValidator" />
</h:inputText>

The converter is not relevant here. It has already done its job of converting String to Measure. The validator will just retrieve the Measure. The @ManagedProperty which you've there will also not work that way. It will basically set the property with result of request.getParameter("measure") and I don't think that there's such a parameter submitted by your form.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555