【发布时间】:2018-09-22 17:59:15
【问题描述】:
我正在尝试针对一组复选框制定自定义规则。请求数据正确输入
cbarray => [
cb1 => null,
cb2 => true,
cb3 => true,
]
如果所有复选框都设置为 true,我正在尝试制定一个失败的验证规则。
然后我将它添加到我的 laravel 规则中
'job-payment.*' => [ new NotAllTrue ],
期望这将作为值数组发送到我的规则中,但它似乎只发送第一个属性和值。
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class NotAllTrue implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return !array_reduce($value, function ($carry, $val) {
return $carry && $val;
}, true);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'These cannot all be true.';
}
}
【问题讨论】:
-
如果不将
.*添加到规则中会怎样? -
就是这样,谢谢@Joe