kwamaking/validator-exception-handler

A very simple exception handler for Symfony's Validator.

v1.0.1 2013-12-11 17:17 UTC

This package is not auto-updated.

Last update: 2022-04-25 13:54:34 UTC


README

A very simple exception handler for Symfony's Validator.

Installation

To install with composer simply require:

"kwamaking/validator-exception-handler": "dev-master"

Don't forget to add the module to your application configuration for ZF2.

Usage

For documentation on how to use Symfony's Validator please see:

http://symfony.com/doc/2.4/book/validation.html

Note: You can pull the annotation style validator from the service locator using the "Validator" key.

The usage is pretty simple. If you follow Symfony's documentation for validating a full object, you can take the response from that and pass it into the exception handler.

$exception_handler = $this->service_locator->get('ValidatorExceptionHandler');//or
$exception_handler = new ConstraintViolationExceptionHandler();
$exception_handler->handleException($violation_list);

When you validate an object with Symfony's Validator, it will hand back a constraint violation list, full of constraint violation objects. This simple tool just grabs the first object in that list, and throws an exception with the violation message.

Why?

The philosophy on user input is that it's prone to error, which makes that expected. The way that I use Symfony's Validator is by filling a model from a restful request. I need to validate the data being sent to me before i persist it to a database. I need to return certain response codes or json response objects if one of the constraints isn't met. This simple utility is designed to handle that.

Final Thoughts

I eventually want to add to this. I'll add custom exceptions based on the individual constraints being violated.