Tapestry5: handling multiple submit buttons with form validation event

In Tapestry5, I have two submit buttons on the form, and I want to also execute the Validation event, how can I achieve this? 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() { 
}
+5
source share
1 answer

As you noticed, the event selectedoccurs before the check, so you cannot put the code of the action handler in the event handlers for the submit buttons. However, you can save the status in these methods and perform the actual action in the form's 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 {
        ...
    }
}
+4
source

All Articles