【问题标题】:Disable Button with JSF when validation of Input interferes当输入验证受到干扰时,使用 JSF 禁用按钮
【发布时间】:2019-02-09 16:23:54
【问题描述】:

我正在使用带有 Primefaces 5.3 的 JSF,并且在启用/禁用命令按钮时遇到问题。

首先,我有这个输入:

<h:inputText id="series" value="#{bean.series}"
    required="false" converterMessage="Please enter three digits" >
    <f:validateRegex pattern="[0-9]{3}" />
    <p:ajax event="keyup" update="download"/>
    </h:inputText>

在backing bean中,这个字段是一个整数

private Integer series;

我的目标是在输入不为空时启用/禁用“下载”按钮。我不在乎它是否无效,但前提是它不包含任何字符。

<h:commandButton id="download" value="Download"
action="#{bean.download}" 
disabled="#{empty bean.series}"/>

更新事件在 keyup 上触发,但由于验证,支持 bean 中的属性 'series' 始终为空。

您能帮我解决一下吗? 谢谢

【问题讨论】:

  • 按钮 ID 在“更新”中不匹配
  • 抱歉,只是编辑错误。我会编辑问题,id是正确的。
  • 尝试将 process="series" 添加到 p:ajax
  • 试过了,不行。调用了系列的吸气剂,但仍然是 null 。基本上我输入 'abcd' 但正则表达式与输入不匹配,因此它没有在 bean 中设置系列的值。如果后备 bean 中的 series 属性仍然为 null,则我的按钮未启用。
  • 正确,没有理由启用您的按钮,因为由于该字段无效,您无论如何都无法提交它。但如果你真的想要这个,创造性地使用stackoverflow.com/questions/17926245/…,你就能得到答案

标签: validation jsf


【解决方案1】:

首先让我声明该要求不合逻辑,因为如果该值无效,则您不能(至少在您的代码没有其他更改的情况下)不能通过按钮提交表单。

但如果你真的想要这个,请使用 Disable/Enable h:commandButton on validation fails/ is ok 中的内容

<h:inputText id="series" value="#{bean.series}"
    required="false" converterMessage="Please enter three digits" binding="#{series}">
   <f:validateRegex pattern="[0-9]{3}" />
   <p:ajax event="keyup" update="download"/>
</h:inputText>

<h:commandButton id="download" value="Download" 
   action="#{bean.download}"  
   disabled="#{ ... EL using 'empty bean.series' and 'series.valid' ..." />

EL 必须由您定义,因为您最清楚空/非空和有效/无效的哪些组合应该使按钮被禁用

【讨论】:

  • 感谢您的回复。要求是当您登陆页面并且输入为空时,应禁用该按钮。在该输入中键入内容后,即使它不是有效的数字而是字母,例如,该按钮也应该显示为已启用。使用此实现,按钮保持禁用状态,即使我在禁用属性如下所示时写了“abc”或“111”:disabled="#{series.valid and empty bean.series}"
  • 我明白这个要求,但还是很奇怪。如果您在 commandButton 之前添加 ` 并更新它会怎样?只是为了检查 EL 是否正确
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-02
  • 2020-04-16
  • 2012-03-06
相关资源
最近更新 更多