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;
|
||||
|
||||
class Model extends \Sudzy\ValidModel {
|
||||
protected $_errors;
|
||||
|
||||
function __construct() {
|
||||
$this->_errors = array();
|
||||
$customValidators = new CustomValidator();
|
||||
parent::__construct($customValidators->init());
|
||||
}
|
||||
@ -13,15 +16,36 @@ class Model extends \Sudzy\ValidModel {
|
||||
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() {
|
||||
$this->setTimestamp();
|
||||
try {
|
||||
parent::save();
|
||||
return true;
|
||||
} catch(\Sudzy\ValidationException $e) {
|
||||
return array_unique($e->getValidationErrors());
|
||||
$this->setError($e->getValidationErrors());
|
||||
} catch(\PDOException $e) {
|
||||
return array($e->getMessage());
|
||||
$this->setError($e->getMessage());
|
||||
} catch(\Exception $e) {
|
||||
$this->setError($e->getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -188,10 +188,17 @@ class Forms {
|
||||
'styles' => $styles
|
||||
));
|
||||
|
||||
if($form->getErrors() === false) {
|
||||
return array(
|
||||
'result' => ($form !== false),
|
||||
'result' => true,
|
||||
'is_widget' => $is_widget
|
||||
);
|
||||
} else {
|
||||
return array(
|
||||
'result' => false,
|
||||
'errors' => $form->getErrors()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function restore($id) {
|
||||
|
@ -438,10 +438,15 @@
|
||||
action: 'saveEditor',
|
||||
data: form
|
||||
}).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(
|
||||
"<%= __('An error occured, please reload the page and try again.') %>"
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user