diff --git a/lib/Models/Model.php b/lib/Models/Model.php index 704bef62e0..94936a06b2 100644 --- a/lib/Models/Model.php +++ b/lib/Models/Model.php @@ -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()); - } catch (\PDOException $e) { - return array($e->getMessage()); + } catch(\Sudzy\ValidationException $e) { + $this->setError($e->getValidationErrors()); + } catch(\PDOException $e) { + $this->setError($e->getMessage()); + } catch(\Exception $e) { + $this->setError($e->getMessage()); } return false; } diff --git a/lib/Router/Forms.php b/lib/Router/Forms.php index a0bcded129..31b53b465f 100644 --- a/lib/Router/Forms.php +++ b/lib/Router/Forms.php @@ -188,10 +188,17 @@ class Forms { 'styles' => $styles )); - return array( - 'result' => ($form !== false), - 'is_widget' => $is_widget - ); + if($form->getErrors() === false) { + return array( + 'result' => true, + 'is_widget' => $is_widget + ); + } else { + return array( + 'result' => false, + 'errors' => $form->getErrors() + ); + } } function restore($id) { diff --git a/views/form/editor.html b/views/form/editor.html index a014ca7ef4..991ed07b62 100644 --- a/views/form/editor.html +++ b/views/form/editor.html @@ -438,10 +438,15 @@ action: 'saveEditor', data: form }).done(function(response) { - if(response === false) { - MailPoet.Notice.error( - "<%= __('An error occured, please reload the page and try again.') %>" - ); + if(response.result === false) { + if(response.errors.length > 0) { + MailPoet.Notice.error(response.errors.join('
')); + } else { + MailPoet.Notice.error( + "<%= __('An error occured, please reload the page and try again.') %>" + ); + } + return false; }