I’m saving a hasmany-relation and i want to display possible validation errors WITH the specific (shortened/trimmed) entity data to the user WITHOUT iterating the whole entity.
I want to show the user a small excerpt from the entity that has caused the validation error.
So when I run the following code in my Controller i get the entity and the validation errors as a nested array.
But what I actually want is the error message AND a excerpt of the entity.
I came up with two solutions, which both feel quite hacky:
iterate the whole nested $entity = $this->Foo->newEntity($data), check each field for errors and write them to an array which later on I can pass to the view.
Map the results of $validationErrors = $entity->getErrors() to the entity. This way I don’t have to loop through the whole entity structure.
Is there a better way to implement this? Which solution would you prefer?
Still feels hacky to me.
I get an array of errors containing the trimmed entity in question.
It looks like the follownig sample. This is actually what I’m trying to achieve: an array of all error messages with the trimmed entity and field that is responsible for the errror. This array will then be passed to the view.
...
[2] => Array
(
[errorMsg] => Array
(
[0] => This field cannot be left empty
[1] => Custom Error
)
[path] => /user/2258/
[field] => name
[entity] => Array
(
[tel] => foo
[nickname] => bar
[some_other_prop] => baz
)
)