Search code examples
tapestry

Tapestry5 : handling multiple submit buttons with form validation event


In Tapestry5, I am having two submit buttons in the form and I want to perform Validation event as well, how can I achieve that ? This is what I am trying to do :

In page.tml

<form t:type="form" t:id="verifyCreateExampleModelForm">

  <input class="btsubmit" t:type="submit" t:id="saveAsAwaitingCompletion" >
  <input class="btsubmit" t:type="submit" t:id="saveAsCreated">
</form>

In page.class

@OnEvent(value = EventConstants.VALIDATE_FORM, component = "verifyCreateExampleModelForm")
private Object validation() {
    if (StringUtils.isEmpty(modelTypeName)) {
        verifyCreateExampleModelForm.recordError("incorrectmodelTypename"));
        this.isAllowed = false;
    }
}

@OnEvent(component = "saveAsAwaitingCompletion", value = "selected")
private void onSaveAsAwaitingCompletion() {
}

@OnEvent(component = "saveAsCreated", value = "selected")
private void onSaveAsCreated() { 
}

Solution

  • As you have observed, the selected event happens before validation, so you can't put your action handler code into the event handlers for the submit buttons. You can, however, store a status in those methods and perform the actual action in the form event handler:

    @OnEvent(component = "saveAsAwaitingCompletion", value = EventConstants.SELECTED)
    void saveAsAwaitingCompletionClicked() {
        this.action = AWAITING_COMPLETION;
    }
    
    @OnEvent(component = "saveAsCreated", value = EventConstants.SELECTED)
    void saveAsCreatedClicked() { 
        this.action = CREATED;
    }
    
    ... //validation logic etc.
    
    @OnEvent(component="verifyCreateExampleModelForm" value = EventConstants.SUCCESS)
    void save() {
        if (this.action == AWAITING_COMPLETION) {
            ...
        } else {
            ...
        }
    }