{"id":15516752,"url":"https://github.com/samrap/laravel-validation","last_synced_at":"2025-07-25T03:04:47.687Z","repository":{"id":62539837,"uuid":"59241241","full_name":"samrap/laravel-validation","owner":"samrap","description":"Lightweight validation for cleaner Laravel controllers","archived":false,"fork":false,"pushed_at":"2016-08-04T21:59:56.000Z","size":22,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"2.0","last_synced_at":"2025-04-23T03:49:17.772Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samrap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-19T20:40:48.000Z","updated_at":"2021-11-17T11:38:07.000Z","dependencies_parsed_at":"2022-11-02T15:33:37.711Z","dependency_job_id":null,"html_url":"https://github.com/samrap/laravel-validation","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samrap%2Flaravel-validation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samrap%2Flaravel-validation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samrap%2Flaravel-validation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samrap%2Flaravel-validation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samrap","download_url":"https://codeload.github.com/samrap/laravel-validation/tar.gz/refs/heads/2.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250366685,"owners_count":21418768,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T10:09:50.113Z","updated_at":"2025-04-23T03:49:22.258Z","avatar_url":"https://github.com/samrap.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Validation\n\n[![StyleCI](https://styleci.io/repos/59241241/shield?style=flat)](https://styleci.io/repos/59241241)\n[![Build Status](https://travis-ci.org/samrap/laravel-validation.svg?branch=master)](https://travis-ci.org/samrap/laravel-validation)\n[![Latest Stable Version](https://poser.pugx.org/samrap/laravel-validation/v/stable)](https://packagist.org/packages/samrap/laravel-validation)\n[![Total Downloads](https://poser.pugx.org/samrap/laravel-validation/downloads)](https://packagist.org/packages/samrap/laravel-validation)\n[![Latest Unstable Version](https://poser.pugx.org/samrap/laravel-validation/v/unstable)](https://packagist.org/packages/samrap/laravel-validation)\n[![License](https://poser.pugx.org/samrap/laravel-validation/license)](https://packagist.org/packages/samrap/laravel-validation)\n\n### Laravel Validation\n---\nLaravel Validation is a bare-bones, minimal validation package for the Laravel framework. Its only purpose is to provide a way to separate your request validation rules from your models and controllers, neither of which should contain such information. It accomplishes this by essentially acting as a broker between your validation rules and a Laravel validator instance.\n\n```php\npublic function store(Request $request, ModelValidator $validator)\n{\n    $validator = $validator-\u003evalidate($request-\u003eall());\n    $this-\u003evalidateWith($validator, $request);\n\n    ...\n}\n```\n\n### But I'm Using Form Requests\n---\nIf you're using [Form Requests](https://laravel.com/docs/5.2/validation#form-request-validation), then this package won't be of use. If you prefer to explicitly validate requests from within your controllers but want to keep your validation rules separate, then give Laravel Validation a try!\n\n### Installation\n---\nInstall via Composer:\n\n`composer require samrap/laravel-validation`\n\nThen add the service provider to your providers array in `config/app.php`:\n\n```php\nSamrap\\Validation\\ValidationServiceProvider::class\n```\n\nFinally, the base `Validator` class needs to be published to a new `app/Validators` directory. This can be done using the `vendor:publish` command:\n\n`php artisan vendor:publish --provider=\"Samrap\\Validation\\ValidationServiceProvider\"`\n\n### Usage\n---\nAll validators reside in the `app/Validators` directory and extend the class `App\\Validators\\Validator`. There should be one validator class per model. For example, the validator for a `User` model could be called `UserValidator`.\n\nLaravel Validation provides a useful artisan command for generating new validators on the fly. Let's create a validator for our `User` model and define some rules:\n\n`php artisan make:validator UserValidator`\n\nThis will create a new `UserValidator` class in the `app/Validators` directory that looks like this:\n\n```php\nnamespace App\\Validators;\n\nuse App\\Validators\\Validator;\n\nclass UserValidator extends Validator\n{\n    /**\n     * The validation rules.\n     *\n     * @var array\n     */\n    protected $rules = [];\n}\n```\n\nEach validator has a `rules` property which (suitably) houses all the validation rules for the intended model. Let's define some basic rules for this validator:\n\n```php\n/**\n * The validation rules.\n *\n * @var array\n */\nprotected $rules = [\n    'email' =\u003e 'email|required|max:255|unique:user',\n    'password' =\u003e 'min:8|confirmed',\n];\n```\n\nGreat! We now have a validator class named `UserValidator` with the rules we intend to validate with in our controller. Let's jump over to the `UserController` and see how to use this new validator class.\n\nFirst, we will want to import this class into our controller:\n\n`use App\\Validators\\UserValidator`\n\nNow, let's validate a POST request for the controller's `store` method:\n\n```php\npublic function store(Request $request, UserValidator $validator)\n{\n    $validator = $validator-\u003evalidate($request-\u003eall());\n    $this-\u003evalidateWith($validator, $request);\n\n    ...\n}\n```\n\nA few things are going on here. Let's go line by line.\n\nFirst, in addition to the current request, we are type hinting an instance of our `UserValidator` as it has dependencies that should be resolved via the service container:\n\n```php\npublic function store(Request $request, UserValidator $validator)\n```\n\nOur validator inherits a `validate` method from its parent class, `Samrap\\Validation\\Validator`, which we can use to obtain an `Illuminate\\Validation\\Validator` instance. Our `validate` method takes the same arguments as if we were [manually creating a validator](https://laravel.com/docs/5.2/validation#manually-creating-validators) using Laravel's `Validator::make` method (more on this later). So, we will simply pass the request input to the `$validator-\u003evalidate()` method:\n\n```php\n$validator = $validator-\u003evalidate($request-\u003eall());\n```\n\nFinally, we can make use of Laravel's `ValidatesRequests` trait, included by default on all controllers. It provides us with a `validateWith` method, which expects a validator instance and the request and will handle redirection if the validation fails:\n\n```\n$this-\u003evalidateWith($validator, $request);\n```\n\nThat's it! That is all you need to do to validate your requests. The validator will use the rules defined in your `UserValidator` to validate the request, in two lines of code in your controller. Obviously, this cleans up your controllers dramatically as the amount of validation you need increases.\n\nOf course, there may be times in a certain request when you need to add to or override some of the rules you defined in your validator. No worries, it's super easy!\n\n```php\n$validator = $validator-\u003evalidate($request-\u003eall(), [\n    'name' =\u003e 'string|required',\n]);\n```\n\nIn this case, we are adding a rule for a `name` field, which will be merged with our rules defined in the `UserValidator`. By default, any rules passed explicitly to the `validate` method will override the rules defined in the validator if they exist.\n\n### Additional Features\n---\n##### Multiple Rulesets\nLaravel Validation expects a `rules` property on your validator class, but it is possible to define additional properties and use those in specific cases. You may have different requirements when updating a record vs storing, or have unique rules if a user is of a specific role.\n\nLet's define an `updating` property on the `App\\Validators\\UserValidator` class with specific rules for updating a user:\n\n```php\nprotected $updating = [\n    // rules...\n];\n```\n\nThen in our controller's `update` method, we can call the validator's `using` method and pass the name of the property we want to validate with:\n\n```php\npublic function update(Request $request, UserValidator $validator)\n{\n    $validator = $validator-\u003eusing('updating')-\u003evalidate($request-\u003eall());\n    $this-\u003evalidateWith($validator, $request);\n\n    ...\n}\n```\n\nBy calling the `using` method before `validate`, we are telling the validator to use the `updating` property instead of the default `rules`.\n\n### Contributing\n---\nContributions are more than welcome! You can submit feature requests to [rapaport.sam7@gmail.com](mailto:rapaport.sam7@gmail.com), or fork the repo yourself!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamrap%2Flaravel-validation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamrap%2Flaravel-validation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamrap%2Flaravel-validation/lists"}