• laravel自定义验证


    1、在控制器中直接写验证
    $this->validate($request, [
    'video_ids' => [
    function($attribute, $value, $fail) {
    $ids = explode(',', $value);
    foreach ($ids as $id) {
    if ($id > 2147483647) {
    $fail(':id max is 2147483647!');
    }
    }
    }
    ]
    ]);

    2、全局自定义方法
    在 app/Providers/AppServiceProvider.php
    use AppValidationsCustomValidation;
     
    public function boot()
    {
    new CustomValidation();
    }

    3、在/resources/lang/en/validation.php定义返回错误提示

    return [
    'max_id' => 'max id is 2147483647'
    ];
    4、在app/Validation/CustomValidation.php
    <?php
    /**
    * Created by PhpStorm.
    * User: ganga
    * Date: 2019/3/7
    * Time: 下午8:00
    */

    namespace AppValidations;

    use IlluminateSupportFacadesValidator;

    class CustomValidation
    {
    public function __construct()
    {
    $this->maxId();
    }

    public function maxId()
    {
    Validator::extend('max_id', function ($attribute, $value, $parameters, $validator) {
    $ids = explode(',', $value);
    foreach ($ids as $id) {
    if ($id > 2147483647) {
    return false;
    }
    }
    return true;
    });
    }
    }

    5 ules中可以写入
    'video_ids' => 'string|nullable|max_id',

     
    https://upeng.github.io/blog/2017/10/18/laravel-validator/



  • 相关阅读:
    mybatis与spring的整合(代码实现)
    使用maven构建一个web项目
    解决maven 找不到指定路径应该如何
    建一个maven项目
    spring xml的配置
    mybatis.xml和mapper.xml的配置
    mvc @helper 创建用户自定义html
    sqlserver CLR sqlserver使用C# dll
    索引碎片
    压缩数据库
  • 原文地址:https://www.cnblogs.com/agang-php/p/10491760.html
Copyright © 2020-2023  润新知