2013-07-21 64 views
0

我對錶單實際子表單的驗證有問題。如何不使用Ajax按鈕驗證表單

在我的網站我有某種表和「添加行」按鈕(BlockingAjaxSubmitLink)。 當我嘗試添加說2行時,我得到驗證錯誤(因爲此表中的行具有Required = True參數),我不能添加另一行。我嘗試過使用簡單的AjaxLink,但它沒有在onClick方法中引用表單,當我完成一些行並單擊「添加行」時,這些數據就會丟失。

我想只在點擊「保存」按鈕後啓用驗證。

任何想法如何處理這個問題?

回答

0

我做了一些像你想要的一樣使用AjaxLink。 我AjaxLink

private AjaxLink addNewRow = new AjaxLink("addNewRow") { 
     @Override 
     public void onClick(AjaxRequestTarget target) { 
     MyEntityObject newTableRowObject = new MyEntityObject(irrelevantParameter); 
     entityObjectTableService.createNewRowInDB(newTableRowObject); 
     target.add(listViewContainer); 
     } 
    }; 

在此代碼listViewContainerWebMarkupContainer含有ListView拿着錶行。

當我點擊此AjaxLink表示我的表中的行的新對象添加到數據庫中和然後將含有ListView容器被刷新刷新ListView和新的空對象正在被從DB提取並示出爲在我的表格結尾新的一行。

0
+0

我設置這個參數,驗證被禁用,但表格(表格)表現得有點奇怪。當我有幾行數據時,我突然添加另一行,所有行都變空了。 我無法驗證無法更新表單。也許我錯過了什麼。 – plancys

0

現在我寫某種黑客

首先,我設置

addKnowledgeLink.setDefaultFormProcessing(false); 

和未來

BlockingAjaxSubmitLink<Object> addKnowledgeLink = new BlockingAjaxSubmitLink<Object>(
       "link_knowledge_add") { 
    @Override 
    protected void onSubmit(AjaxRequestTarget target, Form<?> form) { 
     ChangeDataForm.this.process(this); 

     /* some code */ 


     target.add(form.get(MY_CONTAINER_ID)); 
    } 
(...) 

and my hack ...

//HACK 
public void process(IFormSubmitter object){ 
    if (!isEnabledInHierarchy() || !isVisibleInHierarchy()) 
    { 
     return; 
    } 
    // run validation 
    validate(); 
     /*if (hasError()) 
    { 
     // mark all children as invalid 
     markFormComponentsInvalid(); 
      // let subclass handle error 
     callOnError(object); 
    } 
    else 
    {*/ 
     // mark all children as valid 
     markFormComponentsValid(); 
      // before updating, call the interception method for clients 
     beforeUpdateFormComponentModels(); 
      // Update model using form data 
     updateFormComponentModels(); 
      // validate model objects after input values have been bound 
     onValidateModelObjects(); 
     if (hasError()) 
     { 
      callOnError(object); 
      return; 
     } 
     // Form has no error 
     delegateSubmit(object); 
    //} 
} 

和我ovveride一個方法

@Override 
    protected void onError(){ 
     super.onError(); 
     this.updateFormComponentModels(); 
    } 

我知道這是醜陋的解決方案,但我想不出更好的東西.. ,我不能關機的反饋消息