displaying server side validation error messages on alpaca form

165 Views Asked by At

Could somebody provide a very simple example of how to display a server side validation message on an alpaca form?

E.g. How do I display the messages if the simple form at http://www.alpacajs.org/demos/bootstrap/simple/simple1.html is submitted with an ajax request and the server returns the following (or similar) validation message:

{
  "success":false,
  "errors": [
  {
    "field": "first",
    "message": "first name must be unique"
  },
  { "field": "last",
    "message": "last name must be unique"
  }]
}

I read these issues, but unfortunately I couldn't figure out the solution to this.

1

There are 1 best solutions below

0
On

I finally came up with a simple jQuery DOM manipulation solution:

"submit": {
"title": "Send Form Data",
"click": function(e) {
    var errorDiv = '<div class="help-block alpaca-message" style="color: #a94442;"><i class="glyphicon glyphicon-exclamation-sign"></i>&nbsp;';
    var promise = this.ajaxSubmit();
    promise.done(function(sdata) {
        if (sdata.success == true) {
            window.location.href ="/user/occasion";
            return true;
        }
        $.each(sdata.errors, function(field, message) {
            aField = $('[data-alpaca-field-name="' + field + '"]');
            if (aField.length === 0)
                return true;
            aField.addClass('has-error');
            aField.after(errorDiv + message + "</div>");
        });
    });
    promise.fail(function(sdata) {
        var message = 'Server error!</br>' + sdata.responseText;
        $('.alpaca-container').after(errorDiv + message + "</div>");

    });
}

}