laravel-validators/foundation 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

laravel-validators/foundation

Composer 安装命令:

composer require laravel-validators/foundation

包简介

An opinionated way to register custom validators in Laravel.

README 文档

README

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

An opinionated way to register custom validators in Laravel.

Installation

Begin by installing the package through Composer.

$ composer require laravel-validators/foundation

Create a service provider that extends LaravelValidators\Foundation\ValidationServiceProvider.

<?php

namespace App\Providers;

use LaravelValidators\Foundation\ValidationServiceProvider as ServiceProvider;

class ValidationServiceProvider extends ServiceProvider
{
  /**
   * The validation rules provided by your application.
   *
   * @var array
   */
  protected $rules = [
  //  'gender' => \App\Validators\GenderValidator::class,
  ];

  /**
   * Register the Closure based validators for the application.
   *
   * @return void
   */
  public function rules()
  {
    //
  }
}

Then add the following to the providers array in config/app.php.

App\Providers\ValidationServiceProvider::class

Usage

This package does not add any special functionality, it is essentially a wrapper for the following.

Validator::extend('gender', App\Validators\GenderValidator::class, 'Must be male or female.');

Creating validators

Like Laravel you can define custom validators as either closures or classes. Validators are typically stored in the app/Validators directory; however, you are free to choose your own storage location as long as your commands can be loaded by Composer.

Generating validators

To create a new validator, use the make:validator Artisan command - provided by this package. This command will create a new validator class in the app/Validators directory. Don't worry if this directory does not exist in your application, since it will be created the first time you run the make:validator Artisan command.

$ php artisan make:validator GenderValidator

The validate method will be called when your validator is executed. You may place your validation logic in this method.

Let's take a look at an example validator. Note that we are able to inject any dependencies we need into the validator's constructor. The Laravel service container will automatically inject all dependencies type-hinted in the constructor:

<?php

namespace App\Validators;

class GenderValidator
{
  /**
   * Validate the given value.
   *
   * @param  string  $attribute
   * @param  mixed  $value
   * @param  array  $parameters
   * @param  \Illuminate\Contracts\Validation\Validator  $validator
   * @return  bool
   */
  public function validate($attribute, $value, $parameters, $validator)
  {
    return in_array($value, ['male', 'female']);
  }
}

Finally register the custom validator class in the $rules array in App\Providers\ValidationServiceProvider.

protected $rules = [
  'gender' => \App\Validators\GenderValidator::class,
];

Closure validators

Closure based validators provide an alternative to defining validators as classes. These are defined in the App\Providers\ValidationServiceProvider class using the inherited ->rule() method.

/**
 * Register the Closure based validators for the application
 *
 * @return void
 */
public function rules()
{
  $this->rule('gender', function ($attribute, $value, $parameters, $validator) {
    return in_array($value, ['male', 'female']);
  });
}

Error messages

If your custom validator class provides a public static message() method, it will be used to retrieve the validation message.

<?php

namespace App\Validators;

class GenderValidator
{
  /**
   * Validate the given value.
   *
   * @param  string  $attribute
   * @param  mixed  $value
   * @param  array  $parameters
   * @param  \Illuminate\Contracts\Validation\Validator  $validator
   * @return  bool
   */
  public function validate($attribute, $value, $parameters, $validator)
  {
    return in_array($value, ['male', 'female']);
  }
  
  public function sanitize($value)
  {
    return strtolower($value);
  }

  /**
   * Set the validation error message.
   *
   * @return string
   */
  public static function message()
  {
    return 'You can only specify male or female as your gender.';
  }
}

Sanitize input

If you want to sanitise data before it is validated, you can specify a sanitize method on your validator.

<?php

namespace App\Validators;

class GenderValidator
{
  /**
   * Validate the given value.
   *
   * @param  string  $attribute
   * @param  mixed  $value
   * @param  array  $parameters
   * @param  \Illuminate\Contracts\Validation\Validator  $validator
   * @return  bool
   */
  public function validate($attribute, $value, $parameters, $validator)
  {
    return in_array($value, ['male', 'female']);
  }
  
  /**
   * Sanitize the given value before it is validated.
   *
   * @param mixed $value
   * @return mixed
   */
  public function sanitize($value)
  {
    return strtolower($value);
  }
}

Credits

laravel-validators/foundation 适用场景与选型建议

laravel-validators/foundation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 175 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 08 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「validator」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 laravel-validators/foundation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 laravel-validators/foundation 我们能提供哪些服务?
定制开发 / 二次开发

基于 laravel-validators/foundation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-30