提交表单但忽略required =“true”和f:ajax

时间:2015-04-26 19:26:59

标签: jsf

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

<f:ajax

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

没有<p:commandButton>&gt;它可以正常工作或者<f:ajax>。但不是42>ResolveAssemblyReferences: Primary reference "Microsoft.Synchronization.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL". 42>C:\Program Files (x86)\MSBuild\12.0\bin\amd64\Microsoft.Common.CurrentVersion.targets(1697,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "Microsoft.Synchronization.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [d:\a\src\SomeProject.csproj]

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分: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(在这种情况下为form:noValidationButton)上通过其确切的id检查事件源,并且如果单击“无验证”按钮则返回。它看起来像这样:

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

答案 1 :(得分:0)

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

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