Passing paramater to validator of composite component

286 Views Asked by At

I am using Mojarra 2.3.0. I have a composite component which contains an inputText. I want to be able to validate this inputText using a FacesValidator. Furthermore I want to pass certain parameters to the validator.

My Composite component is defined this way:

<composite:interface>
    ...
    <composite:editableValueHolder name="inputValidator" targets="value" />
    ...
</composite:interface>

<composite:implementation>
    ...
    <h:panelGroup layout="block" styleClass="col-sm-10">
        <h:inputText  id="value"
                      disabled="#{cc.attrs.disabled}"
                      value="#{cc.attrs.value}"
                      styleClass="form-control"
                      required="#{cc.attrs.required}"
                      onchange="#{cc.attrs.onChange}"
        />
    </h:panelGroup>
    ...
</composite:implementation>

I use the component this way and declared a f:validator and f:attribute (which should be passed to the validator)

<uiComps:field labelText="add language"
           id="languages"
           autocompleteValues="#{ocrEngineViewModel.getAllSupportedLanguages()}" >
    <f:ajax event="onChange" listener="#{ocrEngineViewModel.updateSelectedLanguages}" render="newParameter:languages:value newParameter:usedLanguages" />
    <f:validator validatorId="OcrLanguageValidator" for="inputValidator" />
    <f:attribute name="allSupportedLanguages" value="#{ocrEngineViewModel.allSupportedLanguages}"/>
</uiComps:field>
<h:message for="languages" />

My validator looks like this:

@FacesValidator("OcrLanguageValidator")
public class OcrLanguageValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    Object allSupportedLanguages = component.getAttributes().get("allSupportedLanguages");
    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "lala", "not good"));
}

}

There are two problems:

  1. The parameter "allSupportedLanguages" (which is a List<String>) is null when I try to retrieve it in the validator.
  2. The ValidationException which holds a FaceMessage is not displayed in the message area for the component. I don't see any information about the message on the page.

Update:

Instead of the composite component I put the code directly into my page.

<h:panelGroup layout="block" styleClass="col-sm-10">
    <h:inputText  id="value" styleClass="form-control">
        <f:ajax  event="change" listener="#{ocrEngineViewModel.updateSelectedLanguages}" render="newParameter:languages:value newParameter:usedLanguages" />/>
        <f:validator validatorId="OcrLanguageValidator"/>
        <f:attribute name="allSupportedLanguages" value="#{ocrEngineViewModel.allSupportedLanguages}"/>
    </h:inputText>
</h:panelGroup>

This works and I see the attribute in the validator. So I guess the problem lies in the usage of the composite component.

Update2:

I was able to solve the validation error message problem. I placed the <h:message for="value"/> inside the composite component.

0

There are 0 best solutions below