From 9841233d64b6f31bb98f39562f6180854c41ba68 Mon Sep 17 00:00:00 2001 From: JapSeyz Date: Wed, 15 Jun 2016 20:49:44 +0200 Subject: [PATCH] Allow Ajax Validation errors in Frontend.js For some odd reason, this was left out of the frontend, but implemented in App.js and Backend.js --- public/assets/javascript/frontend.js | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/public/assets/javascript/frontend.js b/public/assets/javascript/frontend.js index 442f3233..9a43499b 100644 --- a/public/assets/javascript/frontend.js +++ b/public/assets/javascript/frontend.js @@ -3923,6 +3923,13 @@ function log() { }, error: function(data, statusText, xhr, $form) { $submitButton = $form.find('input[type=submit]'); + + // Form validation error. + if (422 == data.status) { + processFormErrors($form, $.parseJSON(data.responseText)); + return; + } + toggleSubmitDisabled($submitButton); showMessage('Whoops!, it looks like something went wrong on our servers.\n\ Please try again, or contact support if the problem persists.'); @@ -4181,6 +4188,28 @@ $.extend( } }); +function processFormErrors($form, errors) +{ + $.each(errors, function (index, error) + { + var $input = $(':input[name=' + index + ']', $form); + + if ($input.prop('type') === 'file') { + $('#input-' + $input.prop('name')).append('
' + error + '
') + .parent() + .addClass('has-error'); + } else { + $input.after('
' + error + '
') + .parent() + .addClass('has-error'); + } + + }); + + var $submitButton = $form.find('input[type=submit]'); + toggleSubmitDisabled($submitButton); +} + /*! * Smooth Scroll - v1.4.13 - 2013-11-02 * https://github.com/kswedberg/jquery-smooth-scroll