engency/laravel-model-validation
最新稳定版本:v0.3.1
Composer 安装命令:
composer require engency/laravel-model-validation
包简介
Model validation for Laravel projects
关键字:
README 文档
README
Requirements
- PHP 7.1+
- The Laravel framework 7.0+
Installation
You may use composer to install the laravel-model-validation plugin into your Laravel project;
composer require engency/laravel-model-validation
Use the Validatable trait on the models you would like to perform validation on.
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Engency\ModelValidation\Validatable; Class User extends Model { use Validatable; }
Create a new directory in the app directory called 'ModelValidators'. This directory will contain all rules which apply for the concerning models. For each model using the Validatable trait, create a file called {name of the model}ModelValidator.php. A ModelValidator should look like following;
namespace App\ModelValidators; use Engency\ModelValidation\ModelValidator; Class UserModelValidator extends ModelValidator { /** * @return array */ public function rules() : array { return [ 'name' => 'required|string' ]; } }
Validating and creating a new user is now very easy;
$user = User::validateAndCreateNew(['name' => 'John']);
Updating an existing users works almost the same;
$user->validateAndUpdate(['name' => 'John']);
You could add additional sets of rules to each model;
namespace App\ModelValidators; use Engency\ModelValidation\ModelValidator; Class UserModelValidator extends ModelValidator { /** * @return array */ public function rules() : array { return [ 'name' => 'required|string' ]; } public function otherRules() : array { return [ 'name' => 'required|string|min:5', 'age' => 'required|integer|min:22' ]; } }
$user = User::validateAndCreateNew(['name' => 'John', 'age' => 25], 'other'); $user->validateAndUpdate(['name' => 'John', 'age' => 25], 'other');
For a list of all rules, please visit the Laravel Validation documentation; https://laravel.com/docs/validation
Contributors
- Frank Kuipers (GitHub)
License
This plugin is licenced under the MIT license.
统计信息
- 总下载量: 2.67k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-09