使用验证器在UI中的变量属性:重复(Using validator with a variable

2019-08-18 03:27发布

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

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

Code snippet:

<ui:repeat value="#{questionnaire.questionsCollection}"
           var="question" varStatus="status">
  ..
  <h:inputText rendered="#{question.qutyId.ofTypeNumber}"
               value="#{filledQuestionnaire.answersCollection[status.index].answerValue}">
    <f:validateLongRange minimum="#{question.minimumValue}"
                         maximum="#{question.maximumValue}"/>
  </h:inputText>
  ..
</ui:repeat>

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

Answer 1:

这确实是指定/预期的行为。 taghandlers的像的属性<f:validateXxx>期间视图编译时间进行评估。 因此,他们不能参考的变量而言视图时可用渲染时间等的当前迭代变量<ui:repeat> 。 当您在视图生成时像JSTL使用它运行一个迭代它的确会工作<c:forEach> 关于查看生成时间与视图渲染时间更复杂的解释在这里给出: JSTL在JSF2的Facelets ...有道理?

你有基本相同的问题,因为在这里详细解释: 如何为数据表中的每一行集转换属性? 它列出了详细的各种解决方案。 一个在特定情况下的解决方案将使用OmniFaces <o:validator>使渲染时间的所有属性的评价,让您只需更换

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

通过

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

为了得到所期望它的工作。

也可以看看:

  • 设置使用EL验证器属性基于UI:重复VAR


文章来源: Using validator with a variable attribute in ui:repeat