1

I need to show a confirmation/dialog and send the POST request from within the dialogbox only if the user select a radiobutton among other radiobuttons with the value "waiting". In all other cases no confirmation/dialog needs to show up and the POST request needs to get fired directly from the #save element.

Here is the form element in my JSP:

<form:form id="saveform" modelAttribute="form" action="#">
<div class="row form-group radio">
    <label class="col-xs-12 control-label text-right">
        <spring:message code="indicatorX"/> :
    </label>
    <div class="col-xs-12">
        <div class="has-error">
            <form:errors path="indicatorX" cssClass="help-block error" />
        </div>
        <label class="radio-inline radio-inline-espace">
            <form:radiobutton path="indicatorX" value="true" class="js-close" data-collapse="divX"/>
            <spring:message code="yes"/>
        </label>
        <label class="radio-inline radio-inline-espace">
            <form:radiobutton path="indicatorX" value="false" class="js-open" data-collapse="divX"/>
            <spring:message code="no"/>
        </label>
        <label class="radio-inline radio-inline-espace">
            <form:radiobutton path="indicatorX" value="waiting" class="js-close" data-collapse="divX"/>
            <spring:message code="Not responded"/>
        </label>
    </div>
</div>
<div class="row form-group radio">
    <label class="col-xs-12 control-label text-right">
        <spring:message code="indicatorY"/> :
    </label>
    <div class="col-xs-12">
        <div class="has-error">
            <form:errors path="indicatorX" cssClass="help-block error" />
        </div>
        <label class="radio-inline radio-inline-espace">
            <form:radiobutton path="indicatorY" value="true" class="js-close" data-collapse="divY"/>
            <spring:message code="yes"/>
        </label>
        <label class="radio-inline radio-inline-espace">
            <form:radiobutton path="indicatorY" value="false" class="js-open" data-collapse="divY"/>
            <spring:message code="no"/>
        </label>
        <label class="radio-inline radio-inline-espace">
            <form:radiobutton path="indicatorY" value="waiting" class="js-close" data-collapse="divY"/>
            <spring:message code="Not responded"/>
        </label>
    </div>
</div>
<a id="save" class="btn btn-primary" href="#modalConfirmation" data-toggle="modal" data-backdrop="static">
    <spring:message code="saveRecord" />
</a>
<!--  Modal element to confirm if user want to save -->
<div id="modalConfirmation" class="modal" tabindex="-1" role="dialog" data-backdrop="static" data-keyboard="false" data-show="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header"></div>
            <div class="modal-body">
                <spring:message code="MSG-XXX"/>
            </div>
            <div class="modal-footer">
                <button id="confirmYes" class="btn btn-default" data-dismiss="modal">
                    <spring:message code="no"/>
                </button>
                <button id="confirmYes" class="btn btn-primary" data-dismiss="modal">
                    <spring:message code="yes"/>
                </button>
            </div>
        </div>
    </div>
</div>

The POST endpoint in my controller

    @RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@ModelAttribute FormObject form,
    BindingResult validation, RedirectAttributes redirectAttributes, ModelMap model) {
    LOG.info("webapp: method = save");

    //validation logic
    //service logic
    return ...;
}

Please bear in mind that I am taking over a project so haven't designed the architecture and currently I have to deal with some special cases as you can see the button is defined as an anchor element (with id save).

The results achieved so far when trying several examples:

A) The save button will either fire the POST request and show the dialog (while it should not)without waiting for user confirmation

OR

B)If any of the radiobutton has the selected value "waiting" the request is fired but don't reach the server.

I had some difficulty to think how I could achieve that, I saw lot of examples on stack like this example dialogbox ajax on how to send from within a dialogbox but I think in my case I would need some validation logic before sending as an asynchronous call? (AJAX)

Here is my JS code with some simple logic to check if radiobutton has "waiting" value.

    var selectedValOne = $("[name='indicatorX']:checked").val();
    var selectedValTwo = $("[name='indicatorY']:checked").val();

    $('document').on(
      "click",
      "#save",
      function() {

        if (selectedValOne === 'waiting' || selectedValTwo === 'waiting') {
          $("#modalConfirmation").modal("show");

          $('#confirmYes').on('click', function() {
            showProgressbar();
            $("#saveform").attr(
              'action',
              PATH_CONTEXT +
              '/savingcontext/save');
            $("#saveform").submit();

          });

        } else {

          showProgressbar();
          $("#saveform").attr(
            'action',
            PATH_CONTEXT +
            '/savingcontext/save');
          $("#saveform").submit();

        }
      });
halfer
  • 19,824
  • 17
  • 99
  • 186
algorithmic
  • 137
  • 1
  • 4
  • 15
  • 1
    This seems like a front-end problem, can't you just fix it by adding some js that does the logic? – wusatosi Jan 07 '20 at 07:12
  • Thank you for your reply, that is indeed what I thought but when I add the logic to check if the radiobutton has value "waiting" well nothing happens no request is sent to the server nor the onclick event is triggered. I will add my JS code to the question. – algorithmic Jan 07 '20 at 17:08
  • So first issue fixed the button is now triggering and calling the modal and firing the submit request from the yes buttong I had to change the jquery call to $('document').on( "click", "#save", function() {}); – algorithmic Jan 07 '20 at 17:59

1 Answers1

0

The button is now triggering and calling the modal and firing the submit request from the yes button I had to change the jquery call to $('document').on( "click", "#save", function() {});

algorithmic
  • 137
  • 1
  • 4
  • 15