承接 sandaffo/laravel-model-validator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sandaffo/laravel-model-validator

最新稳定版本:v1.0.4

Composer 安装命令:

composer require sandaffo/laravel-model-validator

包简介

A Laravel package to add validation methods directly to Eloquent models

README 文档

README

A Laravel package to add validation rules and messages at the model level.

After install this package, you don't need to write the validation rules inside controller or form request, you can add them inside model and directly use methods to validate.

Adding validation rules inside model itself makes model readable for developers.

Installation

Install the package via composer:

composer require sandaffo/laravel-model-validator

Usage

Adding Validation to a Model

To add validation to a model, define the rules and messages properties in your model:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    // define rules array
    public static $rules = [
        'title' => 'required|string|max:255',
        'slug' => 'required|string|max:255|unique:posts',
        'description' => 'required|string|min:8',
    ];

    // messages are optional, only provide when want customize message
    public static $messages = [
        'title.required' => 'The title field is required.',
        'slug.required' => 'The slug field is required.',
        'description.required' => 'The description field is required.',
    ];
}

Example

Below is an example demonstrating the usage of the package:

$p = new Post();
$p->title = "Test Post One";
$p->slug = "test-post-one";
$p->description = "Test post one description";

if ($p->isValid()) { // $p->isValid() returns true or false
    echo "Post is valid";
    $p->save();
} else {
    echo "Post is not valid";
    print_r($p->errors());
}

Tinker Example

$ php artisan tinker
$p = new Post();
$p->title = "Test Post One";
$p->slug = "test-post-one";
$p->description = "Test post one description";

$p->isValid(); // true or false
$p->errors(); // []

$p->errorMessages(); // [] single dimensional array of all error messages

$p->getRules(); // to get the defined rules if you need somewhere
// [
//     "title" => "required|string|max:255",
//     "slug" => "required|string|max:255|unique:posts",
//     "description" => "required|string|min:8",
// ]

$p->getMessages(); // [] of defined custom error messages

$p->getValidator(); // return Validator object if you need
// Illuminate\Validation\Validator { ... }

$p->getValidator()->errors();
// Illuminate\Support\MessageBag { ... }

$p->save(); // true

$p;
// App\Models\Post {
//     title: "Test Post One",
//     slug: "test-post-one",
//     description: "Test post one description",
//     updated_at: "2024-07-24 08:45:45",
//     created_at: "2024-07-24 08:45:45",
//     id: 1,
// }

Methods

  • isValid(): Checks if the model is valid according to the defined rules.
  • errors(): Returns an array of validation errors.
  • errorMessages(): Returns an array of error messages.
  • getRules(): Returns the validation rules.
  • getMessages(): Returns the validation messages.
  • getValidator(): Returns the validator instance.

License

This package is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-07-23