/** * Formats the messages returned by Respect Validator's NestedValidationException * * Instead of returning all the messages as a simple array it maps the message to the name of the input field so * that way it is easier to parse the messages when they're sent back to the front end. * * Heavily inspired by NestedValidationException->getAllMessages() * @todo make sure $input isn't null * @todo handle collision in keys * @param NestedValidationException $exceptions * @return array */ public function formatMessages(NestedValidationException $exceptions) { $messages = [$exceptions->getFullMessage()]; foreach ($exceptions as $exception) { $input = $exception->getParams()["input"]; $message = $exception->getMessage(); $messages[$input] = $message; } if (count($messages) > 1) { array_shift($messages); } return $messages; }
private function handleValidation(ServerRequestInterface $request, ResponseInterface $response, NestedValidationException $exception) { $body = json_encode(['message' => 'There was a problem when validating', 'errors' => $exception->getMessages()]); return $response->withStatus(400)->withHeader('Content-type', 'application/json')->write($body); }
public function __construct(NestedValidationException $parent) { $this->exceptions = $parent->getRelated(); }