admin管理员组文章数量:1122846
I use the Laravel validator to dynamically validate a field like so:
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
$validator = Validator::make(
data: (array)$parent,
rules: [
'code' => [Rule::in($allowedCodes)],
]
);
if ($validator->fails()) { ... }
However, I want to be able to also allow missing/null values.
How can I do this? Adding null
in the list of $allowedCodes
does not seem to work.
Also, I try to avoid using the string representation of the in
rule (that requires concatenating all allowed codes into a string.
I use the Laravel validator to dynamically validate a field like so:
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
$validator = Validator::make(
data: (array)$parent,
rules: [
'code' => [Rule::in($allowedCodes)],
]
);
if ($validator->fails()) { ... }
However, I want to be able to also allow missing/null values.
How can I do this? Adding null
in the list of $allowedCodes
does not seem to work.
Also, I try to avoid using the string representation of the in
rule (that requires concatenating all allowed codes into a string.
rules: ['nullable', Rule::in($allowedCodes)]
work? – Tim Lewis Commented Nov 22, 2024 at 14:03nullable
rule allows the field to be blank (null
), and if it isn'tnull
, then it should check against theRule::in(...)
logic. What validation errors do you get when it fails? – Tim Lewis Commented Nov 22, 2024 at 14:12本文标签: phpLaravel ValidationHow can I make Rulein() allow missingnull valuesStack Overflow