{"id":14975258,"url":"https://github.com/omnicode/lara-validation","last_synced_at":"2025-10-27T13:30:21.821Z","repository":{"id":57031324,"uuid":"91840991","full_name":"omnicode/lara-validation","owner":"omnicode","description":"Convenient wrapper for laravel validation","archived":false,"fork":false,"pushed_at":"2020-03-20T14:17:23.000Z","size":53,"stargazers_count":2,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T05:41:16.654Z","etag":null,"topics":["lara-validation","laravel","laravel5","laravel5-package","validation"],"latest_commit_sha":null,"homepage":null,"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/omnicode.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":"2017-05-19T19:59:14.000Z","updated_at":"2021-08-06T11:19:01.000Z","dependencies_parsed_at":"2022-08-23T18:50:37.896Z","dependency_job_id":null,"html_url":"https://github.com/omnicode/lara-validation","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omnicode%2Flara-validation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omnicode%2Flara-validation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omnicode%2Flara-validation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omnicode%2Flara-validation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/omnicode","download_url":"https://codeload.github.com/omnicode/lara-validation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238497623,"owners_count":19482288,"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":["lara-validation","laravel","laravel5","laravel5-package","validation"],"created_at":"2024-09-24T13:51:47.163Z","updated_at":"2025-10-27T13:30:21.446Z","avatar_url":"https://github.com/omnicode.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003ca href=\"https://travis-ci.org/omnicode/lara-validation\"\u003e\u003cimg src=\"https://travis-ci.org/omnicode/lara-validation.svg\" alt=\"Build Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/omnicode/lara-validation\"\u003e\u003cimg src=\"https://poser.pugx.org/omnicode/lara-validation/d/total.svg\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/omnicode/lara-validation\"\u003e\u003cimg src=\"https://poser.pugx.org/omnicode/lara-validation/v/stable.svg\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/omnicode/lara-validation\"\u003e\u003cimg src=\"https://poser.pugx.org/omnicode/lara-validation/license.svg\" alt=\"License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n# Lara Validation - wrapper for Laravel Validation\n\nLara Validation is a powerful wrapper for laravel validations (it is influenced by Cakephp 3 validations)\n\nIt has the following advantages\n- More Logical way for defining rules\n- Allows to move validation rules away from controller, service or from other layers to a separate Validation layer\n- Makes validations re-usable between different service layers, controllers etc.\n- Allows to define multiple validations with shared rules\n- Easier to write custom validation messages\n- Better way of defining custom validation methods\n- Convenient features for defining conditional validation rules\n- Easily integrated with Form Requests\n\n## Contents\n\n1. \u003ca href=\"#installation\"\u003eInstallation\u003c/a\u003e\n2. \u003ca href=\"#quick-start\"\u003eQuick start\u003c/a\u003e\n3. \u003ca href=\"#features\"\u003eFeatures\u003c/a\u003e\n\t* \u003ca href=\"#basic-example\"\u003eBasic example\u003c/a\u003e\n\t* \u003ca href=\"#custom-message\"\u003eCustom validation message\u003c/a\u003e\n\t* \u003ca href=\"#conditional-validation-create-update\"\u003eConditional validation during create and update\u003c/a\u003e\n\t* \u003ca href=\"#conditional-validation-callable\"\u003eConditional validation with callable method\u003c/a\u003e\n\t* \u003ca href=\"#add-laravel-rule\"\u003eAdding existing laravel rules\u003c/a\u003e\n\t* \u003ca href=\"#add-custom-rule\"\u003eAdding custom rules\u003c/a\u003e\n\t* \u003ca href=\"#stop-on-first-failure\"\u003eStopping On First Validation Failure\u003c/a\u003e\n\t* \u003ca href=\"#share-rules\"\u003eSharing rules between different validations\u003c/a\u003e\n\t* \u003ca href=\"#form-requests\"\u003eUsing with form requests\u003c/a\u003e\n4. \u003ca href=\"#methods\"\u003eMethods\u003c/a\u003e\n    * \u003ca href=\"#rule-required\"\u003erequired\u003c/a\u003e\n    * \u003ca href=\"#rule-minlength\"\u003eminLength\u003c/a\u003e\n    * \u003ca href=\"#rule-maxlength\"\u003emaxLength\u003c/a\u003e\n    * \u003ca href=\"#rule-email\"\u003eemail\u003c/a\u003e\n    * \u003ca href=\"#rule-numeric\"\u003enumeric\u003c/a\u003e\n    * \u003ca href=\"#rule-unique\"\u003eunique\u003c/a\u003e\n\n## \u003ca id=\"installation\"\u003e\u003c/a\u003eInstallation\n\nAt `composer.json` of your Laravel installation, add the following require line:\n\n``` json\n{\n    \"require\": {\n        \"omnicode/lara-validation\": \"~0.0\"\n    }\n}\n```\n\nRun `composer update` to add the package to your Laravel app.\n\n\n## \u003ca id=\"quick-start\"\u003e\u003c/a\u003eQuick start\n\nDefine simple validator\n\n```\n\u003c?php\nnamespace App\\Validators;\n\nuse LaraValidation\\LaraValidator;\n\nclass PostValidator extends LaraValidator\n{\n    /**\n     * @return \\LaraValidation\\CoreValidator\n     */\n    public function validationDefault()\n    {\n        $this-\u003evalidator\n            -\u003erequired('title')\n            -\u003erequired('content', 'Message can not be empty')\n            -\u003emaxLength('content', 5000);\n\n        return $this-\u003evalidator;\n    }\n\n}\n```\n\nUse it inside your controller or service layer\n\n\n```\n// .. - your namespace\n\nuse App\\Validators\\PostValidator;\n\n// ... controller, service or other class\n\nprotected $postValidator;\n\npublic function __construct(PostValidator $postValidator)\n{\n\t$this-\u003epostValidator = $postValidator;\n}\n\npublic function someMethod()\n{\n\t// $data -\u003e the data, that should be validated - as an array\n\t//        can be taken from request by $request-\u003eall()\n\t//        or can be a custom-created as below\n\n\t$data = [\n\t  'title' =\u003e 'some title',\n\t  'content' =\u003e 'Some content for post'\n\t];\n\t\n\tif ($this-\u003epostValidator-\u003eisValid($data)) {\n\t\t// validated\n\t} else {\n\t\t// not validated\n\t}\n\t\n}\n\n```\n\n\n## \u003ca id=\"Features\"\u003e\u003c/a\u003eFeatures\nLaraValidation has some pre-defined methods, each method has the parameter for providing the field name, possible paramters based on each rule, as well as an optional `$when` parameter which might a callable function, or a string as `create` or `update`. Any laravel  validation rules that do not have wrappers can be easily added by `add` method, which allows also to add custom validation methods as a callable function.\n\n\n### \u003ca id=\"basic-example\"\u003e\u003c/a\u003eBasic Example\n\nTo make the field to be required we can simply write\n```\npublic function validationDefault()\n{\n\t$this-\u003evalidator-\u003erequired('first_name');\n\t\n\treturn $this-\u003evalidator;\n}\n```\n\n### \u003ca id=\"custom-message\"\u003e\u003c/a\u003eCustom validation message\n\n```\n$this-\u003evalidator-\u003erequired('first_name', 'First Name can not be empty');\n\n// For Laravel 5.4 and above you can use\n$this-\u003evalidator-\u003erequired('first_name', __('First Name can not be empty'));\n```\n\n### \u003ca id=\"conditional-validation-create-update\"\u003e\u003c/a\u003eConditional validation during create and update\n\nTo make the rule to be applied only when the record is being created or only when it is being updated\n```\n// the first_name will be required only when creating a record\n$this-\u003evalidator-\u003erequired('first_name', 'First Name can not be empty', 'create');\n\n// the first_name will be required only when updating the record\n$this-\u003evalidator-\u003erequired('first_name', 'First Name can not be empty', 'update');\n```\n\n### \u003ca id=\"conditional-validation-callable\"\u003e\u003c/a\u003eConditional validation with callable method\n\nUse callable method for conditional validation\n\n```\n// the rule will be applied only if the callable method returns true\n$this-\u003evalidator-\u003erequired('first_name', 'First Name can not be empty', function($input) {\n\t$array = $input-\u003etoArray();\t// or you can use getAttributes()\n\treturn empty($array['is_company']) ? true : false;\n});\n```\n\n`$input` is and object of [Illuminate\\Support\\Fluent](https://laravel.com/api/5.4/Illuminate/Support/Fluent.html) that contains the data to be validated.\n\n### \u003ca id=\"add-laravel-rule\"\u003e\u003c/a\u003eAdding existing Laravel rules\n\nIf the rule does not have a wrapper, but it exists in Laravel, it can be easily added by\n\n```\n$this-\u003evalidator-\u003eadd('date_of_birth', 'date')\n```\n\n### \u003ca id=\"add-custom-rule\"\u003e\u003c/a\u003eAdding custom rules\n\nUsing `add` method custom rules by callable methods can be defined\n\n```\n$this-\u003evalidator-\u003eadd('some_field', [\n\t'rule' =\u003e function ($attribute, $value, $parameters, $validator){\n\t\t// logic goes here\n\t\t// return true to apply the validation or false otherwise\n\t}\n], __('Some optional validation message'));\n```\n\nfor the second parameter(in the array), `implicit` option can be defined as well. More info [here](https://laravel.com/docs/5.4/validation#custom-validation-rules)\n\n\n`$attribute`, `$value`, `$parameters` and `$validator` params of the method are defined [here](https://laravel.com/docs/5.4/validation#custom-validation-rules)\n\n### \u003ca id=\"stop-on-first-failure\"\u003e\u003c/a\u003eStopping On First Validation Failure\n\nFor stopping the valudation rules if the given rule fails, use `bail` or its alias `last`\n```\n\t$this-\u003evalidator\n\t\t-\u003enumeric('some_field'\n\t\t-\u003ebail()\n\t\t-\u003eminLength('age', 50)\n\t\t-\u003emaxLength('email', 100);\n\t\n```\nin this case if `some_field` fails to be numeric it will not check for `minLength` or `maxLength` rules\n \n### \u003ca id=\"share-rules\"\u003e\u003c/a\u003eSharing rules between different validations\n\nIt might be cases, that it is required to apply different set of validation rules with different scenarios - meanwhile sharing part of the rules:\n\n```\n// this validation will validate first_name, last_name and email\npublic function validationDefault()\n{\n\t$this-\u003evalidator\n\t\t-\u003erequired('first_name')\n\t\t-\u003erequired('last_name')\n\t\t-\u003erequired('email');\n\t\n\treturn $this-\u003evalidator;\n}\n\n// this validation will validate only first_name and last_name\npublic function validationEdit()\n{\n\t// applies the rules from validationDefault\n\t$this-\u003evalidationDefault();\n\t\n\t// remove existing rule\n\t$this-\u003evalidator\n\t\t-\u003eremove('email');\n\t\n\treturn $this-\u003evalidator;\n}\n\n// this validation will validate first_name, last_name, email and gender\npublic function validationOther()\n{\n\t// applies the rules from validationDefault\n\t$this-\u003evalidationDefault();\n\t\n\t// add new rule\n\t$this-\u003evalidator\n\t\t-\u003erequired('gender');\n\t\n\treturn $this-\u003evalidator;\n}\n```\n\nTo validate the data\n\n```\nuse App\\Validators\\UserValidator;\n\n// ... controller, service or other class\n\nprotected $userValidator;\n\npublic function __construct(UserValidator $userValidator)\n{\n\t$this-\u003euserValidator = $userValidator;\n}\n\npublic function someMethod()\n{\n\t// $data - data to be validated\n\n\t// to validate by `validationDefault` rules use\n\t$this-\u003euserValidator-\u003eisValid($data);\n\t// which is the same as\n\t$this-\u003euserValidator-\u003eisValid($data, ['rule' =\u003e 'default']);\n\n\t// to validate by `validationEdit` rules use\n\t$this-\u003euserValidator-\u003eisValid($data, ['rule' =\u003e 'edit']);\n\n\t// to validate by `validationOther` rules use\n\t$this-\u003euserValidator-\u003eisValid($data, ['rule' =\u003e 'other']);\n}\n```\n\n\n## \u003ca id=\"methods\"\u003e\u003c/a\u003eExisting methods\nHere is the list of predefined methods and wrappers\n\nfor all methods\n- `$name` - field name (required)\n- `$message` - the validation message (optional)\n- `$when` - for conditional validation, can be a string equal to `create`, `update`, `isset`, `notempty`   or a callable method (optional)\n \n### \u003ca id=\"rule-required\"\u003e\u003c/a\u003erequired\n```\npublic function required($name, $message = '', $when = null)\n```\n`$name` can be either string as the field name or array of fields (however in case of array the same error message will be used for all provided fields)\n\n### \u003ca id=\"rule-minlength\"\u003e\u003c/a\u003eminLength\n```\npublic function minLength($name, $length, $message = '', $when = null)\n```\n`$length` mininum number of characters to be allowed\n\n### \u003ca id=\"rule-maxlength\"\u003e\u003c/a\u003emaxLength\n```\npublic function maxLength($name, $length, $message = '', $when = null)\n```\n`$length` maximum number of characters to be allowed\n\n### \u003ca id=\"rule-email\"\u003e\u003c/a\u003eemail\n```\npublic function email($name, $message = '', $when = null)\n```\n\n### \u003ca id=\"rule-numeric\"\u003e\u003c/a\u003enumeric\n```\npublic function numeric($name, $message = '', $when = null)\n```\n\n### \u003ca id=\"rule-unique\"\u003e\u003c/a\u003eunique\n```\npublic function unique($name, $params = [], $message = '', $when = null)\n```\n\n`$params` can be either\n- string - as a db table's exact name\n```\n$this-\u003evalidator-\u003eunique('email', 'users', __('Email already exists. Please restore your password'));\n```\n\n- Model's class, e.g.\n\n```\n$this-\u003evalidator-\u003eunique('field_name', Post::class, __('This value already exists'))\n```\n\n- array, which's first value is the Model's class and the following parameters are columns that should be considered during checking the uniqueness: suppose we need to force unique `title` field per user-basis\n\n```\n$this-\u003evalidator-\u003eunique('title', [Post::class, 'user_id'], __('This title already exists'))\n```\n\n**Important Notice:** the field `user_id` should exist in the validation data\n\n\n## \u003ca id=\"form-requests\"\u003e\u003c/a\u003eUsing with form requests\n\nThe rules defined by LaraValidation can be easily used in Form Requests, for that `rules` and `messages` methods should be used, which return the list of validation rules in native format and the list of messages respectively.\n\n```\n\u003c?php\nnamespace App\\Http\\Requests;\n\nuse App\\Validators\\PostValidator;\n\nclass PostRequest\n{\n    /**\n     * @var PostValidator\n     */\n    protected $postValidator;\n\n    /**\n     * @param PostValidator $postValidator\n     */\n    public function __construct(PostValidator $postValidator)\n    {\n        $this-\u003erules = $postValidator-\u003evalidationDefault()-\u003erules();\n        $this-\u003emessages = $postValidator-\u003evalidationDefault()-\u003emessages();\n    }\n\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomnicode%2Flara-validation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomnicode%2Flara-validation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomnicode%2Flara-validation/lists"}