验证前运行方法后的Laravel验证器
我在下面有验证规则验证前运行方法后的Laravel验证器,laravel,laravel-validation,Laravel,Laravel Validation,我在下面有验证规则 public function rules() { return [ 'ports' => 'required|array|min:2|max:10', 'ports.*.id' => 'required|distinct|exists:ports,id', 'ports.*.order' => 'required|distinct|integer|between:1,10', ]; } 以及
public function rules()
{
return [
'ports' => 'required|array|min:2|max:10',
'ports.*.id' => 'required|distinct|exists:ports,id',
'ports.*.order' => 'required|distinct|integer|between:1,10',
];
}
以及withValidator方法
public function withValidator($validator)
{
// check does order numbers increasing consecutively
$validator->after(function ($validator) {
$orders = Arr::pluck($this->ports, 'order');
sort($orders);
foreach ($orders as $key => $order) {
if ($key === 0) continue;
if ($orders[$key - 1] + 1 != $order) {
$validator->errors()->add(
"ports.$key.order",
__('validation.increase_consecutively', ['attribute' => __('validation.attributes.order')])
);
};
}
});
}
如果客户端没有发送端口
数组,Arr:pull
方法由于$this->ports
等于NULL
而引发异常。在formRequest中完成验证后,如何调用此代码块?尝试写入
if ($validator->fails())
{
// Handle errors
}
前
我想应该是
$validator->fails()
,因为@akcoban希望在所有规则无误通过后,使用validator($validator)运行公共函数
public function withValidator($validator)
{
if ($validator->fails())
{
$orders = Arr::pluck($this->ports, 'order');
sort($orders);
foreach ($orders as $key => $order) {
if ($key === 0) continue;
if ($orders[$key - 1] + 1 != $order) {
$validator->errors()->add(
"ports.$key.order",
__('validation.increase_consecutively', ['attribute' => __('validation.attributes.order')])
);
};
}
}
}