【问题标题】:Submit form but ignore required="true" with f:ajax提交表单但使用 f:ajax 忽略 required="true"
【发布时间】:2015-07-05 02:52:15
【问题描述】:

我有一个简单的表格,如下图所示,带有两个按钮。

<h:form>

    <h:inputText required="#{empty param[save.clientId]}"
        value="#{bean.a}" id="a" />

    <h:inputText required="#{empty param[save.clientId]}"
        value="#{bean.b}" id="b" />

    <h:commandButton binding="#{save}" value="WORKING SAVE JSF"
        action="#{bean.submit}">
    <f:ajax execute="@form" render="@none" />
    </h:commandButton>

    <h:commandButton value="WORKING SUBMIT JSF" action="#{bean.submit}">
    <f:ajax execute="@form" render="@none" />
    </h:commandButton>

一个按钮(保存)应该只将值(以及确切的值)传递给模型没有验证。 第二个(提交)应该验证输入字段,如果没有错误,则传递给模型。

没有&lt;f:ajax> 或&lt;p:commandButton&gt; 也可以。但根本不是&lt;f:ajax&gt;

我怎样才能做到这一点?

【问题讨论】:

  • stackoverflow 上有几个相关(几乎)相同的问题。先试试里面有什么
  • 与我上周回答的这个问题非常相似:stackoverflow.com/questions/29848032/…
  • 它们似乎都不适用于f:ajax
  • 我同意 tt_dev。这都是关于&lt;f:ajax&gt;。没有&lt;f:ajax> 也可以正常工作。查看更新的问题。

标签: jsf


【解决方案1】:

这是一个工作示例。

在 xhtml 级别,您应该为您的输入字段设置一个自定义验证器:

    <h:form id="form">      
        <h:inputText value="#{bean.a}" >
            <f:validator validatorId="myCustomValidator"/>
        </h:inputText>
        <h:inputText value="#{bean.b}" >
            <f:validator validatorId="myCustomValidator"/>
        </h:inputText>

        <h:commandButton id="noValidationButton" action="#{bean.submit}" value="Submit only">
            <f:ajax execute="@form" render="@form" />
        </h:commandButton>
        <h:commandButton action="#{bean.submit}" value="Submit and validate">
            <f:ajax execute="@form" render="@form" />
        </h:commandButton>
        <h:messages/>
    </h:form>

这个自定义验证器类应该通过其在呈现的 html 上的确切 ID(在本例中为form:noValidationButton)检查事件源,如果单击“无验证”按钮则返回。它看起来像这样:

@FacesValidator("myCustomValidator")
public class MyCustomValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        String[] eventSource = ((HttpServletRequest)context.getExternalContext().getRequest()).getParameterMap().get("javax.faces.source");
        if (eventSource != null && 
            eventSource.length > 0 &&
            eventSource[0].equals("form:noValidationButton"))
        {
            return;
        }
        else
        {
            // do regular validation here
            if (value == null || value.equals(""))
            {
                throw new ValidatorException(new FacesMessage("failed."));
            }
        }
    }
}

下面的web.xml 配置确保 jsf 验证所有提交的输入值,即使它们为空。

<context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>true</param-value>
</context-param>

【讨论】:

  • 看起来不错。我现在没有时间测试它(你知道为什么)。我会在几天内发表评论。你能补充一些解释吗?
  • 当然,我第一次提交答案时时间有限。我现在提供了一些解释。
  • 在我的测试页面上,它就像一个魅力!我将在我当前的应用程序中实现这一点。谢谢!
  • 你知道,我如何将&lt;h:dataTable> 中存在的按钮添加到 if 条件?我正在为此苦苦挣扎。那些都有IDs,比如:form:datatableID:index:buttonID
  • @Alexander 只需检查eventSource[0].endsWith("noValidationButton"),不要打扰 id 的前面部分。但请确保您 execute 仅在当前(单击的)行带有 ajax 请求。
【解决方案2】:

要跳过验证,您可以这样做-

<h:inputText required="#{param['req']=='1'}"
    value="#{bean.a}" id="a" />

<h:commandButton binding="#{save}" value="WORKING SAVE JSF"
    action="#{bean.submit}">
<f:ajax execute="@form" render="@none" />
<f:param id="req" value="1"/>
</h:commandButton>

【讨论】:

  • 如何添加第二个按钮,验证正确触发? “不验证”确实有效。
  • 对不起,我无法正确理解您的问题,您能详细说明一下吗?
  • 基本上:两个按钮。一个应该在没有验证的情况下传递值(忽略 requried = true),另一个应该验证并传递值。
  • 因此,根据您传递参数的按钮将检查验证,而您不会传递参数的按钮将跳过验证。
  • 是的。但是第二个按钮看起来如何?我尝试了一些情况,但它们不能正常工作。你可能对我有提示?
猜你喜欢
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多