2

I need to get the value of a <p: selectOneMenu /> for use in a validator via <f:attribute>:

<p:selectOneMenu id="tDocument" value="#{usuarioController.persona.tipoDocumento}">
    <f:selectItem itemLabel="#{msg.selectOne}" itemValue=""/>
    <f:selectItems value="#{tipeListController.tipoIdentificacion}" var="_tDocument" itemValue="#{_tDocument}"/>
</p:selectOneMenu>

<p:inputText id="doc" value="#{usuarioController.persona.num_documento}" required="true" validator="ciRucValidator">
    <f:attribute id="idenType" name="identificationType" value="#{usuarioController.persona.tipoDocumento}" />
</p:inputText>

But when trying to get it in the validator as below I get null:

TipoIdentificacion identificationType = (TipoIdentificacion) component.getAttributes().get("identificationType");

How is this caused and how can I solve it?

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

1 Answers1

1

The model value is set during 4th phase "Update Model Values". However, validators run during 3rd phase "Process Validators". That is thus one phase earlier. It should be obvious that the updated model value of the other component is not available at that moment.

The canonical approach is to just pass the component and then extract the value directly from it via UIInput#getValue() or UIInput#getSubmittedValue() depending on the order of the components.

<p:selectOneMenu binding="#{tDocument}" ...>
    ...
</p:selectOneMenu>
<p:inputText ... validator="ciRucValidator">
    <f:attribute name="tDocument" value="#{tDocument}" />
</p:inputText>

Note that I removed <f:attribute id>, this doesn't exist, and also note that binding example is as-is; very importantingly without a bean property.

You can grab it in the validator as below:

UIInput tDocument = (UIInput) component.getAttributes().get("tDocument");
TipoIdentificacion identificationType = (TipoIdentificacion) tDocument.getValue();
// ...

This also gives you the opportunity to invalidate the other component via setValid(false) if necessary.

See also:

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