本文介绍在 laravel 中如何从控制器向自定义 formrequest 验证类安全、可靠地传递运行时参数(如 `new => true`),以支持“新增/更新”场景的差异化验证逻辑。
在 Laravel 的分层架构实践中,
将业务逻辑拆分为 Controller → Validation Request → Service 是常见且推荐的做法。但默认情况下,FormRequest 仅接收 HTTP 请求数据($request->all()),无法直接访问控制器中动态添加的变量。你尝试使用的 $request->add(['new' => true]) 并非 Laravel 的有效方法(该方法不存在于 Illuminate\Http\Request 或 FormRequest 中),因此会导致调用失败或静默忽略。
✅ 正确做法是使用 merge() 方法——它会将新键值对合并到请求的输入数据中,并确保后续 rules()、messages() 和 validated() 等方法均可访问该字段:
// 在控制器中(如 store 方法)
public function store(AlbumRequest $request, AlbumService $service)
{
// ✅ 正确:使用 merge() 注入运行时参数
$request->merge(['new' => true]);
try {
$service->store($request);
return redirect()->route('admin.web.album.index');
} catch (\Exception $err) {
return back()->withInput()->with('error', $err->getMessage());
}
}然后在 AlbumRequest 中即可正常读取:
class AlbumRequest extends FormRequest
{
public function rules()
{
// ✅ 可直接通过 $this->input() 或 $this->get() 获取
$isNew = $this->boolean('new'); // 推荐:自动类型转换为 bool
// 或 $this->input('new', false);
return $isNew
? [
'title' => 'required|string|max:255',
'cover_image' => 'required|image|mimes:jpg,jpeg,png|max:2048',
]
: [
'title' => 'required|string|max:255',
'cover_image' => 'nullable|image|mimes:jpg,jpeg,png|max:2048',
];
}
public function authorize()
{
return true;
}
}⚠️ 注意事项:
总结:通过 $request->merge(['key' => $value]) 向 FormRequest 动态注入参数,是 Laravel 官方支持、简洁可靠的方式。配合 boolean()、input() 等辅助方法,可轻松实现基于上下文的条件验证逻辑,兼顾代码可读性与架构清晰度。