Closed nipunadodan closed 1 year ago
We used to have a findMessages()
method that was able to do so, but this started to become out of scope for us. We might bring back that feature in the future.
We might be able to expose $e->getRecursiveIterator()
, that will give you a RecursiveIteratorIterator to play with that has all the nesting information you want. The problem is: that PHP interface is way too confusing.
I'm keeping this open, because it is something I believe we should have. I can't do it for the next release, but it should be on our radar soon.
I realized you can already do that using the RecursiveExceptionIterator
we provide.
I added a test case for that scenario which shows how to get the message and level on the tree for each exception in detail:
https://github.com/Respect/Validation/blob/2.3/tests/integration/issue-1376.phpt
TL;DR
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Exceptions\RecursiveExceptionIterator;
try {
$postValidator->assert($input);
} catch (NestedValidationException $e) {
$explorer = new RecursiveIteratorIterator(
new RecursiveExceptionIterator($e),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($explorer as $innerException) {
var_dump([
'level' => $explorer->getDepth(),
'message' => $innerException->getMessage(),
]);
}
}
This can be useful if you don't want getMessages()
to flatten the messages choosing which ones to display for you.
I'm closing this now, since apparently there are no needed changes.
Let me know if that works for you! If it doesn't, please feel free to reopen this.
I will check this out and will let you know if it doesn't work. Thank you very much for this!!
It's an awesome library to leave for this hard to get right Exception handling and painful documentations. I've got
getFullMessage()
returned as expected but it's just as a String.$e->getMessages()
only outputs one error message from one key/attribute, which shouldn't be the case.With
$e->getMessages()
_(after jsonencoded)With
$e->getFullMessage()
The later has more information. Is there a way I can get all of the information shown with
$e->getMessages()
? Moreover, I think this is a bug. Isn't it?