|
|
@@ -934,7 +934,7 @@ trait EntityTrait
|
|
|
*
|
|
|
* ```
|
|
|
* // Sets the error messages for multiple fields at once
|
|
|
- * $entity->setErrors(['salary' => ['message'], 'name' => ['another message']);
|
|
|
+ * $entity->setErrors(['salary' => ['message'], 'name' => ['another message']]);
|
|
|
* ```
|
|
|
*
|
|
|
* @param array $fields The array of errors to set.
|
|
|
@@ -943,11 +943,27 @@ trait EntityTrait
|
|
|
*/
|
|
|
public function setErrors(array $fields, $overwrite = false)
|
|
|
{
|
|
|
+ if ($overwrite) {
|
|
|
+ foreach ($fields as $f => $error) {
|
|
|
+ $this->_errors[$f] = (array)$error;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
foreach ($fields as $f => $error) {
|
|
|
$this->_errors += [$f => []];
|
|
|
- $this->_errors[$f] = $overwrite ?
|
|
|
- (array)$error :
|
|
|
- array_merge($this->_errors[$f], (array)$error);
|
|
|
+
|
|
|
+ // String messages are appended to the list,
|
|
|
+ // while more complex error structures need their
|
|
|
+ // keys perserved for nested validator.
|
|
|
+ if (is_string($error)) {
|
|
|
+ $this->_errors[$f][] = $error;
|
|
|
+ } else {
|
|
|
+ foreach ($error as $k => $v) {
|
|
|
+ $this->_errors[$f][$k] = $v;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return $this;
|