0

on my jsf page I use a composite component which is basically an input field. My backing bean holds a list that contains all allowed languages which the user is allowed to enter. On submit I want to validate the user's input based on that list.

So I need to pass the list to the validator because obtaining that list takes to long, so I do it when the backing bean is initialized. But I struggle here.

My composite component looks like this:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:composite="http://java.sun.com/jsf/composite">
  <composite:interface>
    <composite:attribute name="value" required="true" />
    <composite:editableValueHolder name="nameValidator" targets="value" />
  </composite:interface>

  <composite:implementation>
    <h:panelGroup>
        <h:inputText id="value" value="#{cc.attrs.value}" />
    </h:panelGroup>
  </composite:implementation>
</html>

My validator look like this:

@FacesValidator("OcrLanguageValidator")
public class OcrLanguageValidator implements Validator {

@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    Object param1 = context.getExternalContext().getRequestParameterMap().get("param1");
    if (param1 != null) {

    }
}

}

And I am using the component like this in my jsf page:

<h:form id="newParameter" styleClass="form-horizontal">
  <uiComps:testCC value="test" >
    <f:validator validatorId="OcrLanguageValidator" for="nameValidator" />
    <f:param name="param1" value="blub" />
  </uiComps:testCC>
</h:form>

When the validator method is called the param1 is always null. How can I achieve my goal? I use Mojarra 2.3.

Edit because of possible duplication: I don't think, that this is a duplication. I am using a composite component here. Without it is a way to do it. But I don't like the idea of adding a special attribute to the component just because I need it in one single case but not the others. Hope there might be a better solution to this problem.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Martin
  • 852
  • 7
  • 20
  • Possible duplicate of [Can I make a JSF Validator with options specified in the JSF markup?](https://stackoverflow.com/questions/8946920/can-i-make-a-jsf-validator-with-options-specified-in-the-jsf-markup) – Jasper de Vries Oct 04 '18 at 07:33
  • I don't think that this is a duplicate because I use a composite component here. Without a composite component your posted link might be a way to do it. But I don't like the idea of adding a new attribute to the composite component just because I need it in one single case and not in all the other cases. I hope there might be another way. – Martin Oct 04 '18 at 07:53
  • Your param is outside the validator, is that 'right' (just 'guessing' here). And you use a param instead of an attribute.... – Kukeltje Oct 04 '18 at 08:57
  • @Kukeltje I guess so. if I put it inside the validator, the validation is not executed at all. – Martin Oct 04 '18 at 09:05
  • In the duplicate they use an attribute instead of a param. Please check that too! – Kukeltje Oct 04 '18 at 09:31
  • Yep. I tried that. If I put an attribute inside the validator it's also not executed. – Martin Oct 04 '18 at 09:34

0 Answers0