ConstraintViolationListInterface to Exception in Symfony



PHP Snippet 1:

class ValidationException extends \Exception
{
    private $violations;

    public function __construct(array $violations)
    {
        $this->violations = $violations;
        parent::__construct('Validation failed.');
    }

    public function getMessages()
    {
        $messages = [];
        foreach ($this->violations as $paramName => $violationList) {
            foreach ($violationList as $violation) {
                $messages[$paramName][] = $violation->getMessage();
            }
        }
        return $messages;
    }

    public function getJoinedMessages()
    {
        $messages = [];
        foreach ($this->violations as $paramName => $violationList) {
            foreach ($violationList as $violation) {
                $messages[$paramName][] = $violation->getMessage();
            }
            $messages[$paramName] = implode(' ', $messages[$paramName]);
        }
        return $messages;
    }
}

PHP Snippet 2:

try {
    $errors = $validator->validate(...);
    if (0 !== count($errors)) {
        throw new ValidationException($errors);
    }
} catch (ValidationException $e) {
    // Here you can obtain your validation errors. 
    var_dump($e->getMessages());
}

PHP Snippet 3:

protected function violationsToArray(ConstraintViolationListInterface $violations)
{
    $messages = [];

    foreach ($violations as $constraint) {
        $prop = $constraint->getPropertyPath();
        $messages[$prop][] = $constraint->getMessage();
    }

    return $messages;
}

PHP Snippet 4:

    $messages = [];

    foreach ($violations as $prop => $constraint) {
        // $prop will not contain any value and this will not work as expected
        $messages[$prop][] = $constraint->getMessage();
    }

PHP Snippet 5:

namespace AppBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Validator\ConstraintViolationListInterface;

/**  
 * The order.placed event is dispatched each time an order is created
 * in the system.
 */
class ConstraintViolationsEvent extends Event
{
    const VIOLATIONS_DETECTED = 'constraint_violations.detected';

    protected $constraintViolationList;

    public function __construct(ConstraintViolationListInterface $constraintViolationList)
    {
        $this->constraintViolationList = $constraintViolationList;
    }

    public function getConstraintViolationList()
    {
        return $this->constraintViolationList;
    }
}

PHP Snippet 6:

class MyController extends Controller
{
    public function myFormAction(Request $request)
    {
        /** handle the request, get the form data, validate the form...etc. **/
        $event = new ConstraintViolationsEvent($constraintViolationList);
        $dispatcher->dispatch(ConstraintViolationsEvent::VIOLATIONS_DETECTED, $event);
    }
}

PHP Snippet 7:

/** @var ConstraintViolationList $violations */
$violations = $this->validator->validate($object);
if ($violations->count()) {
    throw new \Exception('Validation failed: '.$violations);
}