Skip to content

Commit c654918

Browse files
committed
Improved Error Messages when Ajax Enabled.
Replaced "Something went wrong! Please try again." with backend errors.
1 parent 834a263 commit c654918

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

helper.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ public static function submitForm($ajax = false) {
246246
}
247247
if(!empty($errors)) {
248248
$app = JFactory::getApplication();
249+
$send = false;
249250
//showing all the validation errors
250251
foreach ($errors as $error) {
251252
$app->enqueueMessage(\JText::_($error), 'error');
@@ -256,7 +257,13 @@ public static function submitForm($ajax = false) {
256257
}
257258

258259
if ($send !== true) {
259-
throw new \Exception(JText::_('MOD_JDSCFEMAIL_SEND_ERROR'));
260+
switch($params->get('ajaxsubmit'))
261+
{
262+
case 0: throw new \Exception(JText::_('MOD_JDSCFEMAIL_SEND_ERROR'));
263+
break;
264+
case 1: throw new \Exception(json_encode($errors));
265+
break;
266+
}
260267
}
261268
$message = $params->get('thankyou_message', '');
262269
if (empty($message)) {
@@ -277,7 +284,7 @@ public static function submitForm($ajax = false) {
277284
}
278285
$app->redirect($return);
279286
}
280-
return ['message' => $message, 'redirect' => $redirect_url];
287+
return ['message' => $message, 'redirect' => $redirect_url, 'errors' => json_encode($errors)];
281288
}
282289

283290
public static function renderVariables($variables, $source) {
@@ -398,17 +405,17 @@ public static function uploadFile($name, $src) {
398405
else
399406
{
400407
$tmppath = JPATH_SITE . '/tmp';
401-
if(!file_exists($tmppath.'/jdscf')){
408+
if (!file_exists($tmppath.'/jdscf')) {
402409
mkdir($tmppath.'/jdscf',0777);
403410
}
404411
$folder = md5(time().'-'.$filename.rand(0,99999));
405-
if(!file_exists($tmppath.'/jdscf/'.$folder)){
412+
if (!file_exists($tmppath.'/jdscf/'.$folder)) {
406413
mkdir($tmppath.'/jdscf/'.$folder,0777);
407414
}
408415
$dest = $tmppath.'/jdscf/'.$folder.'/'.$filename;
409416

410417
$return = null;
411-
if (JFile::upload($src, $dest)){
418+
if (JFile::upload($src, $dest)) {
412419
$return = $dest;
413420
}
414421
return $return;

tmpl/default.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@
134134
contentType: false,
135135
processData: false,
136136
success: function (response) {
137-
138137
if (response.status == 'success') {
139138
$('.jd-simple-contact-message-<?php echo $module->id; ?>').html(response.data.message);
140139
_loading.addClass('d-none');
@@ -144,8 +143,13 @@
144143
}, 2000);
145144
}
146145
} else {
147-
_loading.addClass('d-none');
148-
showMessage<?php echo $module->id; ?>("error", "<?php echo JText::_("MOD_JDSCF_AJAX_ERROR_ON_SUBMIT") ?>");
146+
var errors = JSON.parse(response.message);
147+
_loading.addClass('d-none');
148+
149+
for (index = 0; index < errors.length; ++index) {
150+
showMessage<?php echo $module->id; ?>("error", errors[index]);
151+
}
152+
149153
}
150154
},
151155
error: function (response) {

0 commit comments

Comments
 (0)