提交表单,但忽略required ="true"与f:ajax [英] Submit form but ignore required="true" with f:ajax

查看:76
本文介绍了提交表单,但忽略required ="true"与f:ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表单,如下面的表单,有两个按钮.

I have a simple form like the one below with two buttons.

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

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

One button (save) should only pass the values (and exactly the values) to the model without validation. The second one (submit) should validate the input fields and if there is no error, pass to the model.

它可以在没有>或<p:commandButton>的情况下工作.但根本不使用<f:ajax>

It does work without <f:ajax> or with <p:commandButton>. But simply not with <f:ajax>

我该如何实现?

推荐答案

这是一个有效的示例.

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

on xhtml level, you should set a custom validator to your input fields:

    <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)检查事件源,并在单击不验证"按钮时返回.它看起来像这样:

this custom validator class should check the event source by its exact id on rendered html (form:noValidationButton in this case), and just return if the button for "no validation" is clicked. it will look like this:

@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验证所有提交的输入值,即使它们保留为空.

the web.xml configuration below makes sure that jsf validates all submitted input values, even if they are left empty.

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

这篇关于提交表单,但忽略required ="true"与f:ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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