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 ?
You should implement a
Validator.E.g.
You can declare it by the input component’s
validatorattributeor 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)The converter is not relevant here. It has already done its job of converting
StringtoMeasure. The validator will just retrieve theMeasure. The@ManagedPropertywhich you’ve there will also not work that way. It will basically set the property with result ofrequest.getParameter("measure")and I don’t think that there’s such a parameter submitted by your form.