【问题标题】:Model value of first input component is null in validator of second component第一个输入组件的模型值在第二个组件的验证器中为空
【发布时间】:2015-10-19 10:38:30
【问题描述】:

我需要通过<f:attribute> 获取<p: selectOneMenu /> 的值以便在验证器中使用:

<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>

但是当试图在下面的验证器中获取它时,我得到null

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

这是怎么引起的,我该如何解决?

【问题讨论】:

  • 尝试使用component.getAttributes().get("idenType")而不是component.getAttributes().get("identificationType"),使用属性的id
  • 我尝试使用组件ID而不是属性名称,但我仍然得到同样的错误。
  • 这些被表单元素包裹了吗?
  • f:attribute 无论如何都不支持id 属性。而且,如果它不在表单中,验证器就不会首先被击中。
  • 谢谢你是对的

标签: validation jsf jsf-2 primefaces


【解决方案1】:

模型值在第 4 阶段“更新模型值”期间设置。但是,验证器在第三阶段“流程验证器”中运行。因此,这是较早的一个阶段。很明显,此时其他组件的更新模型值不可用。

规范的方法是只传递组件,然后根据组件的顺序通过UIInput#getValue()UIInput#getSubmittedValue() 直接从中提取值。

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

请注意,我删除了&lt;f:attribute id&gt;this doesn't exist,还请注意binding 示例是原样的; very importantingly without a bean property.

您可以在验证器中获取它,如下所示:

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

这也让您有机会在必要时通过setValid(false) 使其他组件无效。

另见:

【讨论】:

    猜你喜欢
    • 2018-09-14
    • 2014-12-27
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多