Har*_*ist 0 php validation laravel
我很难理解这个验证规则。基本上,我有两个字段,它们都是nullable. 但是,一旦两个字段都被填满,它们就必须彼此不同。例如,我不能test同时输入它们。如果我填写both字段,则此验证规则有效。
但是,当我只填写one字段时,验证失败,并表示这些字段应该彼此不同,并显示以下消息:
The name and replace must be different.
Run Code Online (Sandbox Code Playgroud)
我检查了提交给我的表单请求的内容,如下:
"name" => null
"replace" => "test"
Run Code Online (Sandbox Code Playgroud)
我的验证规则的精简版本:
public function rules()
{
return [
'name' => 'different:replace|nullable',
'replace' => 'different:name|nullable',
];
}
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释一下我对这个验证规则的误解吗?null值不符合此规则吗?
如果你看一下validateDifferent来自Illuminate\Validation\Concerns\ValidatesAttributes( vendor/laravel/framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php:432) 规则的函数:
public function validateDifferent($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'different');
foreach ($parameters as $parameter) {
$other = Arr::get($this->data, $parameter);
if (is_null($other) || $value === $other) {
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
正如您在 if 情况中看到的,如果另一个值为 null,则规则将失败。
if (is_null($other) || $value === $other)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6243 次 |
| 最近记录: |