Tapestry5 : handling multiple submit buttons with form validation event
Asked Answered
C

1

7

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() { 
}
Carmon answered 24/2, 2012 at 13:47 Comment(0)
T
8

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 {
        ...
    }
}
Tablespoon answered 25/2, 2012 at 17:17 Comment(2)
hey Henning, we are using that pattern with a non-@Persistent this.action property and sometimes it's empty. this is quite non-deterministic and browser-dependent and happens when somehow the requests are dispatched to different jetty-threads. is the action required to be @Persistent or something?Oppression
@Oppression The SELECTED event and the SUCCESS event should be part of the same form submission request, no? If so, then there should be no need for @Persistent.Tablespoon

© 2022 - 2024 — McMap. All rights reserved.