在 ui:repeat 中使用带有变量属性的验证器 [英] Using validator with a variable attribute in ui:repeat

查看:26
本文介绍了在 ui:repeat 中使用带有变量属性的验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 com.sun.faces 版本 2.1.18.在我的应用程序中,我有一个动态的问题列表.我使用 来呈现每个问题.根据问题的类型,我呈现了一种输入组件和验证.如果是数字范围问题,我将 一起使用.

I'm using com.sun.faces version 2.1.18. In my application I have a dynamic list of questions. I use <ui:repeat> to render each question. Depending on the type of question I render a type of input component and validation. In case of a number range question I use <h:inputText> with <f:validateLongRange>.

我遇到的问题是 上的 minimummaximum 属性总是设置为第一个问题的最小值和最大值.因此,当您在任何其他问题上使用验证器时,第一个问题就会失败.这是应该发生的吗?有没有办法对动态生成的组件进行验证?希望不用切换到就能解决.

The problem I run into is that the minimum and maximum attributes on the <f:validateLongRange> are always set to the first question's minimum and maximum value. So, when you use the validator on any other then the first question it fails. Is that supposed to happen? Is there a way to get validation working on dynamically generated components? I hope it can be solved without switching to <c:forEach>.

代码片段:

<ui:repeat value="#{questionnaire.questionsCollection}"
           var="question">
  ..
  <h:inputText value="..">
    <f:validateLongRange minimum="#{question.minimumValue}"
                         maximum="#{question.maximumValue}"/>
  </h:inputText>
  ..
</ui:repeat>

我已经输出了 #{question.minimumValue}#{question.maximumValue},它们为我的问题提供了正确的值.

I've outputted #{question.minimumValue} and #{question.maximumValue}, and they have the correct values for my question.

推荐答案

这确实是指定/预期的行为. 等标记处理程序的属性在视图构建期间进行评估.因此,它们不能像 的当前迭代变量一样引用仅在视图呈现期间可用的变量.当您使用在视图构建期间运行的迭代器(如 JSTL <c:forEach>)时,它确实会起作用.此处给出了有关视图构建时间与视图渲染时间的更详细说明:JSF2 Facelets 中的 JSTL... 有道理吗?

This is indeed specified/expected behavior. The attributes of taghandlers like <f:validateXxx> are evaluated during view build time. So they can't refer a variable which is only available during view render time like the currently iterated variable of <ui:repeat>. It would indeed work when you use an iterator which runs during view build time like JSTL <c:forEach>. A more elaborate explanation about view build time versus view render time is given here: JSTL in JSF2 Facelets... makes sense?

您遇到的问题与此处详细说明的基本相同:如何为数据表的每一行设置转换器属性? 详细概述了各种解决方案.在您的特定情况下的解决方案之一是使用 OmniFaces <o:validator> 启用所有属性的渲染时评估,以便您可以替换

You have basically the same problem as explained in detail here: How to set converter properties for each row of a datatable? It outlines various solutions in detail. One of the solutions in your particular case would be using OmniFaces <o:validator> which enables render-time evaluation of all attributes, so that you can just replace

<f:validateLongRange minimum="#{question.minimumValue}"
                     maximum="#{question.maximumValue}" />

<o:validator validatorId="javax.faces.LongRange" 
             minimum="#{question.minimumValue}"
             maximum="#{question.maximumValue}" />

为了让它按预期工作.

另见:

这篇关于在 ui:repeat 中使用带有变量属性的验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆