【问题标题】:Why does required="true" not fail validation when disabled="true" is specified为什么指定 disabled="true" 时 required="true" 不会验证失败
【发布时间】:2013-07-30 10:56:38
【问题描述】:
<a4j:outputPanel id="tapalSectionSendToPanel" ajaxsingle="true">
    <h:inputText id="sendToId1"  value="#{MainBean.SectionBean.sendTo}" 
        class="createresizedTextbox" 
        required="true" requiredMessage="#{msg.labl_required}"
        disabled="true" />
 <h:message for="sendToId1" style="color:red" />
</a4j:outputPanel> 

我需要验证文本框以进行空验证,并且当我单击按钮而不在文本框中输入任何值时应该显示必需。没有disabled="true" 也能正常工作。我的要求有什么替代方案。

【问题讨论】:

  • 您有冲突的要求:disabled="true" 不允许用户输入任何值,除了显示之外,它会被忽略。那么,为什么要在此处附加验证器?
  • 另外,您的 JSF 中有几个无效属性:autosubmitshowRequiredrequiredMessage 不会在这里做任何事情,class 可能应该是 styleClass
  • 我已经删除了验证器,我的 jsf 有什么问题
  • 你不能“测试”这个。允许用户更改值,然后删除disabled="true"。或者您只是向用户显示(已经设置的)值,那么required="true" 毫无意义。在这种情况下,您可以只显示您想要的文本(使用 rendered="#{empty MainBean.SectionBean.sendTo}" 表示此文本)。
  • 我有一个 Rich:treenode 和一组用户名,点击它会在这个文本框中填充名称,这是为特定用户名发送消息的用户名,因此不点击 treenode 我必须提出消息让用户选择用户名,因此我想要验证器

标签: jsf-2 richfaces


【解决方案1】:

首先,requireddisabled 不能很好地结合在一起,因为根据JSF Spec,它们是互斥的:

  • 必需:指示用户需要为此输入组件提供提交值的标志。
  • disabled:指示此元素不得获得焦点或包含在后续提交中的标志。

就像我在 cmets 中所说,当用户尝试提交表单而不选择节点时,您可以只显示一条消息:

<h:inputText id="sendToId1"  value="#{MainBean.SectionBean.sendTo}" 
    styleClass="createresizedTextbox" required="true" readonly="true" />
<h:message for="sendToId1" value="#{msg.labl_required}" 
    rendered="#{facesContext.postback and facesContext.validationFailed}" />

作为替代方案,您可以在标记中的任何位置显示文本:

<h:outputText value="#{msg.labl_required}" 
    rendered="#{empty MainBean.SectionBean.sendTo}" />

【讨论】:

    【解决方案2】:

    disabled="true" 禁用输入(因此在提交表单时会跳过它),如果您不希望用户输入它,请使用readonly="readonly"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多