我不知道这是我的睡眠不足还是缺少明显的东西,但是我遇到了这个问题:我创建了一个自定义验证规则,我需要通过构造函数传递一个请求值以执行验证。这是我的设置:自定义验证规则/** Rules/RouteValidationRule.php */namespace App\Rules;use Illuminate\Contracts\Validation\Rule;class RouteValidationRule implements Rule { private $action; public function __construct(string $action) { $this->action = $action; // doing dd($action) output the correct value. } // a dd('Hi'); inside here is just ignored: public function passes($attribute, $value) { if ($this->action === 'complete') return (isset($value['delivery_route'])) && (count($value['delivery_route']) >= 2); else return true; } public function message() { /** */ }}表格申请/** UpdateOrderRequest.php */namespace App\Http\Requests;use App\Rules\OrderStatusesRule;use App\Rules\RouteValidationRule;use Illuminate\Foundation\Http\FormRequest;class UpdateOrderRequest extends FormRequest { public function authorize() { /** */ } public function rules() { return [ 'action' => ['required', new OrderStatusesRule], 'meta.delivery_route' => [ 'array', new RouteValidationRule($this->get('action')), // <--- ], ]; }}但事实是,该值已到达自定义规则构造函数,但没有到达该passes()方法,它只是忽略了这一点,就好像可以继续进行一样。PD:我已经检查了每个步骤dd()。提前致谢。更新/** MyController.php */ /** * @param UpdateOrderRequest $request * @return mixed */ public function handle(UpdateOrderRequest $request) // the RequestForm is injected { dd('I passed'); // <-- is reaching this statement when it shouldn't }
2 回答
翻过高山走不出你
TA贡献1875条经验 获得超3个赞
您的规则无法运行的原因是,您要测试的属性不在Request
或null中。如果你想运行规则无论是现在还是不行,有您的规则执行ImplicitRule
不Rule
如:
class RouteValidationRule implements ImplicitRule {
- 2 回答
- 0 关注
- 167 浏览
添加回答
举报
0/150
提交
取消