This project is no longer being maintained., (*1)
Entity Validation for Laravel-Doctrine ORM
Provides integration between standard Doctrine entities and the Laravel Validator
by using Hydrators., (*2)
Requirements
- PHP 7+
- Laravel 5.2+
- laravel-doctrine/orm
Installation
Install using composer, or checkout / pull the files from github.com., (*3)
- composer install somnambulist/laravel-doctrine-entity-validation
- add the service provider to your config/app.php file
- php artisan vendor:publish
Two new config files will added:, (*4)
- doctrine_hydrators.php
- doctrine_validation.php
Add the entity class names to the hydrators config file to have hydrators made.
Add a mapping between the entity and a rules class in validation to allow the factory
class to create Validator instances., (*5)
The validation rules should implement the EntityRules contract or extend:
Somnambulist\EntityValidation\AbstractEntityRules
class. The rules class should contain
the basic rules needed to validate the entity. This is NOT form validation! These
rules are the basic requirements for your domain entities to be valid., (*6)
The validation rules can then be added to your form requests or validation rules.
E.g.: a User entity may have EntityRules requiring a name, email and username but in
the AddUserFormRequest, Roles and Permissions may be additionally required. The
entity rules would look something like:, (*7)
class UserEntityRules extends AbstractRules
{
public function supports($entity)
{
return $entity instanceof User;
}
protected function buildRules($entity)
{
return [
'name' => 'required|min:1',
'email' => 'required|email|unique:User,email,' . ($entity->getId() ?: 'null'),
'username' => 'required|alphanum|unique:User,username,' . ($entity->getId() ?: 'null'),
];
}
}
As the entity is passed in, you can access any method and create complex rules., (*8)
The entity validation factory can then be type-hinted or fetched from the container:, (*9)
class SomeClass ...
{
public function __construct(EntityValidationFactory $validationFactory)
{
$this->factory = $validationFactory;
}
public function someMethod()
{
if ($this->factory->validate($user)) {
}
}
}
Generating Hydrators
An extra command is provided to make generating the hydrators easier:, (*10)
php artisan doctrine:generate:hydrators
These will be cached to the file system in the storage/cache/hydrators folder by
default. Configure the storage folder in the hydrators config file., (*11)
This command can be added to composer install|update
so that the hydrators are
created automatically as changes are made or code deployed., (*12)
-
Note: it is not a requirement to cache the hydrators, however it offers much
better performance in production.
-
Note: it is good to add doctrine:generate:proxies before the hydrators.
Links