views:

528

answers:

1

How can I change the error message of jQuery validation so when it validates the error is composed as bullet points inside a jQuery Ui modal dialog.

Tried this but it doesn't work as expected (i.e. the modal dialog only apperas once, but after it is closed, I can't bring the modal dialog back when submitting invalid data for second time).

Any tips will be appreciated. Thank you.

...
        $("#myform").validate({
          debug: true,
          errorPlacement: function(error, element) {
            error.prependTo('#dialog-message');
          },
          errorLabelContainer: $("dialog-message ul"),
          onblur: false,
          onkeyup: false,
          onsubmit: true,
          wrapper: "li",
          showErrors: function() {
            //$("#dialog").dialog("destroy");
            $("#dialog-message").dialog({
              modal: true,
              buttons: {
                Ok: function() {
                  $(this).dialog('close');
                }
              }
            });
            this.defaultShowErrors();
          }
        });
...

      <div id="dialog-message" title="Download complete">
        <p>test</p>
      </div>
A: 

Change this line:

$("#dialog-message").dialog({

To this:

$("#dialog-message").dialag('destroy').dialog({

This will destroy/re-create the widget correctly, or alternatively...

Run this on document.ready:

$(function() {
  $("#dialog-message").dialog({
     modal: true,
     autoOpen: false,
     buttons: { Ok: function() {
         $(this).dialog('close');
       }
     }
  });
});

And call just this in your validation, this way you're just opening the same dialog widget again:

      showErrors: function() {
        $("#dialog-message").dialog('open');
        this.defaultShowErrors();
      }
Nick Craver
Hi Nick, thanks for the hints. It's working. Now, my other problem is the jQuery validation keeps appending validation error msg to the <div id="error-message"> area. And also it's keep updating the error msg as that's what the default behavior of jQuery validation (I believe). Is there a way for me to capture the error message just once and ignore the dynamic changes performed by jQuery validation. I only want to display the error msg every time user click submit. Again, many thanks.