PHPackages                             rimote/rimote-validation-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. rimote/rimote-validation-bundle

ActiveSymfony-bundle[Validation &amp; Sanitization](/categories/validation)

rimote/rimote-validation-bundle
===============================

Extends and simplifies Symfony's Validator by providing a flat array with error messages.

v1.0.3(9y ago)3161MITPHPPHP &gt;=5.6

Since Mar 29Pushed 9y ago1 watchersCompare

[ Source](https://github.com/EnnioWolsink/RimoteValidationBundle)[ Packagist](https://packagist.org/packages/rimote/rimote-validation-bundle)[ RSS](/packages/rimote-rimote-validation-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (7)Used By (0)

RimoteValidationBundle
======================

[](#rimotevalidationbundle)

Extends and simplifies Symfony's Validator by providing a flat array with error messages.

Installation
------------

[](#installation)

Install using composer:

```
composer require rimote/rimote-validation-bundle

```

Edit `/app/AppKernel.php` and add the following bundle in the AppKernel::registerBundles() method inside the `$bundles` array:

```
new Rimote\ValidationBundle\RimoteValidationBundle()

```

How does it work?
-----------------

[](#how-does-it-work)

Whenever you want to validate a [Doctrine](http://www.doctrine-project.org/projects/orm.html) Entity in your [Symfony](http://symfony.com) codebase, instead of using Symfony's [Validator component](http://symfony.com/doc/current/validation.html) directly, you can use the RimoteValidationBundle's Validator instead.

The RimoteValidationBundle works nearly the same as Symfony's native Validator. Meaning you write your constraints straight into your Entity at the property level and then use the validator's `validate()` method to check if all property values are in the right format.

The difference is the format of the exception that will be thrown in case of validation errors. This will contain a `getErrors()` public method you can use to retrieve a flat key/value array of error messages. The keys will correspond with the Entity properties they relate to, the values will be the error messages as defined in your Entity.

Usage
-----

[](#usage)

First fetch an instance of the Rimote validator via the service container. Inside a container aware Controller, this can be accomplished like this:

```
$validator = $this->get('rimote.validator');

```

Now you can simply pass an instance of an Entity that needs to be validated to the validator's `validate()` method:

```
$validator->validate($cat);

```

If all is good this returns `true`. Otherwise an exception will be thrown, of the type `Rimote\ValidationBundle\Validator\Exception\ErrorMessagesException`. This exception has a public method called `getErrors()`, which produces a flat array with error messages, like so:

```
array(2) {
    'property_1' =>
    string(30) "This value should not be null."
    'property_2' =>
    string(30) "This value should not be null."
}

```

In the context of a REST API, this array of error messages can be returned as a JSON response with HTTP code 500. For a detailed example of such an implementation, see below.

### Example

[](#example)

In this example we have an Doctrine Entity called `Cat`, which has three properties that shouldn't be blank: `name`, `fur_type` and `gender`:

```

```

Here's how we can validate this inside our CatsController::createAction (which uses the [FOSRestBundle](http://symfony.com/doc/current/bundles/FOSRestBundle/index.html) to set the POST body to an instance of `Cat`):

```
