Laravel中如何验证嵌套数组请求?按组类型自定义验证属性
Laravel 嵌套分组属性类型验证实现方案
要实现根据每个group的type验证其下属qualities的type这一需求,可通过自定义验证规则结合Laravel的数组验证能力完成,具体步骤如下:
1. 定义分组与属性类型的映射关系
先明确不同分组类型允许的属性类型,用数组存储映射:
$allowedQualities = [ 'human' => ['hair', 'height'], 'cat' => ['hair'], ];
2. 创建自定义验证规则
通过Artisan命令生成规则类:
php artisan make:rule GroupQualitiesValidation
在生成的App\Rules\GroupQualitiesValidation类中实现验证逻辑:
namespace App\Rules; use Illuminate\Contracts\Validation\Rule; class GroupQualitiesValidation implements Rule { protected $groupType; // 接收当前分组的类型 public function __construct($groupType) { $this->groupType = $groupType; } public function passes($attribute, $value) { $allowedQualities = [ 'human' => ['hair', 'height'], 'cat' => ['hair'], ]; // 若分组类型不在映射中,直接验证失败 if (!isset($allowedQualities[$this->groupType])) { return false; } // 遍历每个属性,检查类型是否在允许列表内 foreach ($value as $quality) { if (!in_array($quality['type'], $allowedQualities[$this->groupType])) { return false; } } return true; } // 自定义错误提示信息 public function message() { $allowed = implode(', ', $allowedQualities[$this->groupType] ?? []); return "分组类型为 {$this->groupType} 的属性只能使用以下类型:{$allowed}"; } }
3. 在验证流程中应用规则
方式一:控制器中直接验证
use App\Rules\GroupQualitiesValidation; use Illuminate\Support\Facades\Validator; // 待验证的Payload $payload = [ 'groups' => [ [ 'type' => 'human', 'qualities' => [ ['type' => 'hair', 'value' => 'blue'], ['type' => 'height', 'value' => '188cm'], ] ], [ 'type' => 'cat', 'qualities' => [ ['type' => 'hair', 'value' => 'yellow'] ] ] ] ]; // 基础验证规则 $rules = [ 'groups' => 'required|array', 'groups.*.type' => 'required|string|in:human,cat', 'groups.*.qualities' => 'required|array', 'groups.*.qualities.*' => 'required|array:type,value', 'groups.*.qualities.*.type' => 'required|string', 'groups.*.qualities.*.value' => 'required|string', ]; // 初始化验证器 $validator = Validator::make($payload, $rules); // 为每个分组的qualities添加自定义验证规则 foreach ($payload['groups'] as $index => $group) { $validator->addRules([ "groups.{$index}.qualities" => new GroupQualitiesValidation($group['type']) ]); } // 执行验证 if ($validator->fails()) { // 处理验证失败,比如返回错误信息 dd($validator->errors()->all()); } // 验证通过后的业务逻辑 echo "验证通过";
方式二:表单请求类中验证
创建表单请求类:
php artisan make:request ValidateGroupsRequest
在App\Http\Requests\ValidateGroupsRequest中编写规则并添加自定义验证:
namespace App\Http\Requests; use App\Rules\GroupQualitiesValidation; use Illuminate\Foundation\Http\FormRequest; class ValidateGroupsRequest extends FormRequest { public function authorize() { // 根据需求调整权限逻辑 return true; } public function rules() { return [ 'groups' => 'required|array', 'groups.*.type' => 'required|string|in:human,cat', 'groups.*.qualities' => 'required|array', 'groups.*.qualities.*' => 'required|array:type,value', 'groups.*.qualities.*.type' => 'required|string', 'groups.*.qualities.*.value' => 'required|string', ]; } // 在验证器初始化后添加自定义规则 public function withValidator($validator) { foreach ($this->input('groups') as $index => $group) { $validator->addRules([ "groups.{$index}.qualities" => new GroupQualitiesValidation($group['type']) ]); } } }
之后在控制器中直接使用该请求类即可:
use App\Http\Requests\ValidateGroupsRequest; public function store(ValidateGroupsRequest $request) { // 请求已通过验证,处理业务逻辑 $validated = $request->validated(); // ... }
内容的提问来源于stack exchange,提问作者James Haraldson
相关产品推荐
相关产品推荐

