How to don't validate form with Ajax buttons

末鹿安然 提交于 2019-12-12 02:29:26

问题


I have a problem with validation on form actually sub-form.

In my website I have some kind of table and "Add row" button (BlockingAjaxSubmitLink). When I try add let say 2 rows, I get validation error (because row in this table has Required=True parameter) and I can't add another row. I tried use simple AjaxLink but it doesn't have reference to form in onClick method and when I complete some rows and click "Add row" this data get lost.

I want to enable validation only after "save" button click.

Any idea how to deal with this problem?


回答1:


I do something like you want using an AjaxLink. My AjaxLink:

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

In this code the listViewContainer is a WebMarkupContainer which contains a ListView holding the table rows.

When i click this AjaxLink a new object representing a row in my table is added to the database and then the container containing the ListView is being refreshed refreshing the ListView and the new empty object is being fetched from the DB and shown as a new row in my table at the end.




回答2:


Depending on your structure maybe you are looking after disabling validation using setDefaultFormProcessing(true); - http://ci.apache.org/projects/wicket/apidocs/6.x/org/apache/wicket/markup/html/form/AbstractSubmitLink.html#setDefaultFormProcessing%28boolean%29




回答3:


For now I write some kind of hack

First I set

addKnowledgeLink.setDefaultFormProcessing(false);

and next

    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);
    //}
}

and I ovveride one method

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

I know it is ugly solution but I couldn't figure out anything better.. And I couldn't shutdown feedback messages



来源:https://stackoverflow.com/questions/17772714/how-to-dont-validate-form-with-ajax-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!