Skip to content

Commit

Permalink
If an entity has error messages we set the property _hasErrors to tru…
Browse files Browse the repository at this point in the history
…e. We use this property to check if the Entity can be saved without going trough all the errors.
  • Loading branch information
JoshuaLuckers committed Jul 4, 2018
1 parent 8d72a2d commit b21c5f3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Datasource/EntityInterface.php
Expand Up @@ -30,6 +30,7 @@
* @method $this setDirty($property, $isDirty)
* @method bool isDirty($property = null)
* @method array getErrors()
* @method bool hasErrors()
* @method array getError($field)
* @method array setErrors(array $fields, $overwrite = false)
* @method array setError($field, $errors, $overwrite = false)
Expand Down
18 changes: 18 additions & 0 deletions src/Datasource/EntityTrait.php
Expand Up @@ -97,6 +97,13 @@ trait EntityTrait
*/
protected $_errors = [];

/**
* Returns whether this entity contains errors.
*
* @var bool
*/
protected $_hasErrors = false;

/**
* List of invalid fields and their data for errors upon validation/patching
*
Expand Down Expand Up @@ -911,6 +918,16 @@ public function getErrors()
->toArray();
}

/**
* Returns whether this entity contains errors.
*
* @return bool
*/
public function hasErrors()
{
return $this->_hasErrors;
}

/**
* Returns validation errors of a field
*
Expand Down Expand Up @@ -943,6 +960,7 @@ public function getError($field)
*/
public function setErrors(array $fields, $overwrite = false)
{
$this->_hasErrors = true;
if ($overwrite) {
foreach ($fields as $f => $error) {
$this->_errors[$f] = (array)$error;
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Table.php
Expand Up @@ -1902,7 +1902,7 @@ public function save(EntityInterface $entity, $options = [])
'_primary' => true
]);

if ($entity->getErrors()) {
if ($entity->hasErrors() === true) {
return false;
}

Expand Down

0 comments on commit b21c5f3

Please sign in to comment.