Coder Social home page Coder Social logo

dave-redfern / laravel-doctrine-entity-validation Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 17 KB

Integration between standard Doctrine entities and Laravels Validation framework

License: MIT License

PHP 100.00%
laravel-doctrine hydrator entity laravel-validation entity-validation laravel

laravel-doctrine-entity-validation's Introduction

This project is no longer being maintained.

Entity Validation for Laravel-Doctrine ORM

Provides integration between standard Doctrine entities and the Laravel Validator by using Hydrators.

Requirements

  • PHP 7+
  • Laravel 5.2+
  • laravel-doctrine/orm

Installation

Install using composer, or checkout / pull the files from github.com.

  • 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:

  • 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.

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.

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:

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.

The entity validation factory can then be type-hinted or fetched from the container:

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:

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.

This command can be added to composer install|update so that the hydrators are created automatically as changes are made or code deployed.

  • 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

laravel-doctrine-entity-validation's People

Contributors

dave-redfern avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.