{"id":14975301,"url":"https://github.com/theriddleofenigma/laravel-model-validation","last_synced_at":"2025-10-27T13:30:25.801Z","repository":{"id":56645073,"uuid":"141806198","full_name":"theriddleofenigma/laravel-model-validation","owner":"theriddleofenigma","description":"Laravel model validation validates the data on model events. *Only for laravel applications.","archived":false,"fork":false,"pushed_at":"2024-04-05T14:58:57.000Z","size":57,"stargazers_count":33,"open_issues_count":1,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T05:41:16.733Z","etag":null,"topics":["database","eloquent","eloquent-models","laravel","laravel-application","laravel-framework","model","model-validation","php","validation"],"latest_commit_sha":null,"homepage":"https://theriddleofenigma.github.io/laravel-model-validation/","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/theriddleofenigma.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-21T11:28:17.000Z","updated_at":"2024-10-01T03:48:56.000Z","dependencies_parsed_at":"2024-04-05T15:50:56.794Z","dependency_job_id":"7204d2e3-16b7-44b2-af8e-5f8bd1bf1a86","html_url":"https://github.com/theriddleofenigma/laravel-model-validation","commit_stats":{"total_commits":53,"total_committers":4,"mean_commits":13.25,"dds":"0.15094339622641506","last_synced_commit":"d5d6703c96718be23a3deab0c3e37a96b5fde77d"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theriddleofenigma%2Flaravel-model-validation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theriddleofenigma%2Flaravel-model-validation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theriddleofenigma%2Flaravel-model-validation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theriddleofenigma%2Flaravel-model-validation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theriddleofenigma","download_url":"https://codeload.github.com/theriddleofenigma/laravel-model-validation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238497655,"owners_count":19482294,"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":["database","eloquent","eloquent-models","laravel","laravel-application","laravel-framework","model","model-validation","php","validation"],"created_at":"2024-09-24T13:51:50.473Z","updated_at":"2025-10-27T13:30:25.449Z","avatar_url":"https://github.com/theriddleofenigma.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\u003ccode\u003e\u0026hearts; Made with \u0026lt;love/\u0026gt; And I love \u0026lt;code/\u0026gt;\u003c/code\u003e\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://app.fossa.com/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation?ref=badge_shield\" alt=\"FOSSA Status\"\u003e\u003cimg src=\"https://app.fossa.com/api/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation.svg?type=shield\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n# Laravel Model Validation\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation?ref=badge_shield)\n\nModel validation - Validates the model data. *Only for laravel applications.\n\nAn easy validator option for your eloquent models. Also have flexibility for additional codes that might be executed on before and after validation.\n\n### Composer install\n```shell\ncomposer require theriddleofenigma/laravel-model-validation\n```\n\n## Usage Examples\nHere user model is mentioned as an example. You could use this in any model you want.\n\n### User.php model\n```php\nuse Enigma\\ValidatorTrait;\n\nclass User extends Model\n{\n    use ValidatorTrait;\n\n    /**\n     * Boot method.\n     */\n    public static function boot()\n    {\n        parent::boot();\n\n        // Add this method for validating the current model on model saving event\n        static::validateOnSaving();\n    }\n\n    public $validationRules = [\n        'name' =\u003e 'required|max:10',\n        'email' =\u003e 'required|email',\n    ];\n\n    public $validationMessages = [\n        'name.required' =\u003e 'Name field is required.',\n        'email.email' =\u003e 'The given email is in invalid format.',\n    ];\n\n    public $validationAttributes = [\n        'name' =\u003e 'User Name'\n    ];\n\n    /**\n     * Code to be executed before the validation goes here.\n     */\n    public function beforeValidation()\n    {\n        // Some code goes here..\n    }\n\n    /**\n     * Code to be executed after the validation goes here.\n     */\n    public function afterValidation()\n    {\n        // Some code goes here..\n    }\n}\n```\n\n### Control the data get validated\nYou can control the data which gets validated by adding validationData method.\n```php\n/**\n * Validation data to be validated.\n *\n * @return array\n */\npublic function validationData(array $data)\n{\n    // Here $data is the value of $this-\u003egetAttributes(), feel free to use your own code to produce the data. Ex: $this-\u003etoArray(), $this-\u003egetOriginal(), etc.,\n    $data[\"name\"] = strtolower($data[\"name\"]);\n\n    // Note: This wouldn't affect your actual model data which is going to persist in DB.\n    \n    return $data;\n}\n```\n\n### Other options\nYou could mention the validation rules, attributes and messages as a property as well as method.\n```php\n/**\n * Validation rules to validate.\n *\n * @return array\n */\npublic function validationRules()\n{\n    // You can process your code here and return the rules as however you want.\n    return [\n        'name' =\u003e 'required|max:10',\n        'email' =\u003e 'required|email',\n    ];\n}\n\n/**\n * Custom messages to replace the validation messages.\n *\n * @return array\n */\npublic function validationMessages()\n{\n    // You can process your code here and return the messages as however you want.\n    return [\n        'name.required' =\u003e 'Name field is required.',\n        'email.email' =\u003e 'The given email is in invalid format.',\n    ];\n}\n\n/**\n * Custom attribute names to replace the validation attribute name.\n *\n * @return array\n */\npublic function validationAttributes()\n{\n    return [\n        'name' =\u003e 'User Name'\n    ];\n}\n```\n\nYou could mention the validation only for creating itself or on any model event just add `$model-\u003evalidate()`.\n```php\n/**\n * Boot method.\n */\npublic static function boot()\n{\n    parent::boot();\n\n    // You can mention like this for validating the model on custom events as your wish\n    self::creating(function($model){\n        $model-\u003evalidate();\n    });\n\n    // Or you can make use of the alias `self::validateOnCreating()`.\n}\n```\n\nRefer the available methods in the ValidationTrait.\n\n## License\n\nLaravel Model Validation is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).\n\n\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation?ref=badge_large)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheriddleofenigma%2Flaravel-model-validation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftheriddleofenigma%2Flaravel-model-validation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheriddleofenigma%2Flaravel-model-validation/lists"}