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 App\Validations\CustomValidation;
 
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 App\Validations;

use Illuminate\Support\Facades\Validator;

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\rules中可以写入
'video_ids' => 'string|nullable|max_id',

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



转载于:https://www.cnblogs.com/agang-php/p/10491760.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值