Better Error handling for models
- added (array)getErrors() to models, returns false if no errors - converted Forms::saveEditor method to use getErrors - added error handling on the form editor view
This commit is contained in:
@ -4,7 +4,10 @@ namespace MailPoet\Models;
|
|||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class Model extends \Sudzy\ValidModel {
|
class Model extends \Sudzy\ValidModel {
|
||||||
|
protected $_errors;
|
||||||
|
|
||||||
function __construct() {
|
function __construct() {
|
||||||
|
$this->_errors = array();
|
||||||
$customValidators = new CustomValidator();
|
$customValidators = new CustomValidator();
|
||||||
parent::__construct($customValidators->init());
|
parent::__construct($customValidators->init());
|
||||||
}
|
}
|
||||||
@ -13,15 +16,36 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
return parent::create();
|
return parent::create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getErrors() {
|
||||||
|
if(empty($this->_errors)) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return $this->_errors;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setError($error = '') {
|
||||||
|
if(!empty($error)) {
|
||||||
|
if(is_array($error)) {
|
||||||
|
$this->_errors = array_merge($this->_errors, $error);
|
||||||
|
$this->_errors = array_unique($this->_errors);
|
||||||
|
} else {
|
||||||
|
$this->_errors[] = $error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function save() {
|
function save() {
|
||||||
$this->setTimestamp();
|
$this->setTimestamp();
|
||||||
try {
|
try {
|
||||||
parent::save();
|
parent::save();
|
||||||
return true;
|
return true;
|
||||||
} catch (\Sudzy\ValidationException $e) {
|
} catch(\Sudzy\ValidationException $e) {
|
||||||
return array_unique($e->getValidationErrors());
|
$this->setError($e->getValidationErrors());
|
||||||
} catch (\PDOException $e) {
|
} catch(\PDOException $e) {
|
||||||
return array($e->getMessage());
|
$this->setError($e->getMessage());
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
$this->setError($e->getMessage());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -188,10 +188,17 @@ class Forms {
|
|||||||
'styles' => $styles
|
'styles' => $styles
|
||||||
));
|
));
|
||||||
|
|
||||||
|
if($form->getErrors() === false) {
|
||||||
return array(
|
return array(
|
||||||
'result' => ($form !== false),
|
'result' => true,
|
||||||
'is_widget' => $is_widget
|
'is_widget' => $is_widget
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
return array(
|
||||||
|
'result' => false,
|
||||||
|
'errors' => $form->getErrors()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function restore($id) {
|
function restore($id) {
|
||||||
|
@ -438,10 +438,15 @@
|
|||||||
action: 'saveEditor',
|
action: 'saveEditor',
|
||||||
data: form
|
data: form
|
||||||
}).done(function(response) {
|
}).done(function(response) {
|
||||||
if(response === false) {
|
if(response.result === false) {
|
||||||
|
if(response.errors.length > 0) {
|
||||||
|
MailPoet.Notice.error(response.errors.join('<br />'));
|
||||||
|
} else {
|
||||||
MailPoet.Notice.error(
|
MailPoet.Notice.error(
|
||||||
"<%= __('An error occured, please reload the page and try again.') %>"
|
"<%= __('An error occured, please reload the page and try again.') %>"
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user