2017-08-29 109 views
1

驗證失敗時,我想通過忽略第二個提交中的無效字段來提交表單。的分離的問題如下:跳過第二個表單提交的無效字段

我的形式由兩個所需輸入foobar,其在另外一個驗證和h:hiddenInput名爲MyValidator自定義驗證,其例如確保的輸入的不等式。如果MyValidator失敗,我改爲提交另一個提交按鈕,該按鈕跳過h:hiddenInput及其驗證程序,只處理輸入foobar

<h:form> 
    <h:panelGroup layout="block" id="fooBarWrapper"> 
     <p:inputText value="#{myControl.foo}" binding="#{foo}" required="true"/> 
     <p:inputText value="#{myControl.bar}" binding="#{bar}" required="true"/> 
    </h:panelGroup> 

    <h:inputHidden validator="myValidator" binding="#{myValidation}"> 
     <f:attribute name="foo" value="#{foo}"/> 
     <f:attribute name="bar" value="#{bar}"/> 
    </h:inputHidden> 

    <p:messages/> 

    <p:commandButton action="#{myControl.doSomething()}" value="Do something" 
        process="@form" update="@form" 
        rendered="#{myValidation.valid}"/> 

    <p:commandButton action="#{myControl.doAnotherThing()}" value="Do another thing" 
        process="fooBarWrapper" update="@form" 
        rendered="#{not myValidation.valid}" 
        oncomplete="if (!args.validationFailed) { console.log('valid'); }"/> 
</h:form> 

@Named 
@FacesValidator("myValidator") 
public class MyValidator implements Validator { 

    @Override 
    public void validate(FacesContext ctx, UIComponent component, Object value) throws ValidatorException { 

     String foo = (String) ((UIInput) component.getAttributes().get("foo")).getValue(); 
     String bar = (String) ((UIInput) component.getAttributes().get("bar")).getValue(); 

     if (foo != null && bar != null && foo.equals(bar)) { 
      throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Foo and bar must not be equal", "")); 
     } 
    } 
} 

@Named 
@ViewScoped 
public class MyControl implements Serializable { 

    private String foo; 
    private String bar; 

    public void doSomething() { 
     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Did something")); 
    } 

    public void doAnotherThing() { 
     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Did another thing")); 
    } 

    ... 
} 

然而,第二個按鈕的動作不叫,但現在MyValidator跳過預期。有趣的是,PrimeFaces args.validationFailed表示驗證成功。

任何人都可以解釋,爲什麼#{myControl.doAnotherThing()}沒有被調用,雖然沒有驗證似乎失敗?

+0

您是否嘗試在第二個'commandButton'上設置'immediate =「true」'? –

+1

那麼你可以簡單地設置'process =「fooBarWrapper」'',在'process'屬性的值中執行''##{p:component('fooBarWrapper')}'是什麼。另外,如果你調用一個參數少的bean方法,不需要添加'()'。 –

+0

(1.)'-'是我的'javax.faces.SEPARATOR_CHAR',因爲我不需要在任何jQuery選擇器中轉義它。然而,'process =「fooBarWrapper」'在這裏是絕對足夠的。我更新了我的問題。謝謝! (2.)我看不到'immediate =「true」'可以幫助我。正如https://stackoverflow.com/a/12961162中指出的那樣,它會忽略所有不具有'immediate =「true」'的輸入。但我確實希望將它們包含在我的表單提交中。 – stan

回答

1

該問題與h:inputHidden的驗證器無關。 你可以更多地去除你的例子。

<h:form> 
    <h:panelGroup layout="block" id="fooBarWrapper"> 
     <p:inputText value="#{myControl.foo}"/> 
     <p:inputText value="#{myControl.bar}"/> 
    </h:panelGroup> 

    <p:messages/> 

    <p:commandButton action="#{myControl.doAnotherThing()}" value="Do another thing" process="fooBarWrapper" update="@form"/> 
</h:form> 

這個例子也行不通。 爲了使這個工作,你也必須處理commandButton。

<p:commandButton action="#{myControl.doAnotherThing()}" value="Do another thing" process="@this fooBarWrapper" update="@form"/> 
相關問題