自定义规则原创
# 创建文件
php artisan make:rule XXXRule
# 文件代码
点击查看
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class PhoneRule implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
## 返回判断结果
return preg_match_all('/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|17[0-9]|18[0-9])\d{8}$/', $value);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return '手机号码格式有误!';
}
}
# 使用
点击查看
'phone' => ['required', 'string', new Uppercase],
上次更新: 2022/08/23, 18:12:45