Maybe you can create a ConstraintViolationsEvent like this :
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;
}
}
Then you can create a listener for this event, and inside this listener, you create your Exception based on all the violations found. Each time that you will find violations you just dispatch your event inside your controller like this :
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);
}
}
In fact, you can manage the creation of your Exception inside a service and call the service in the listener. It is up to you.