HTML form has select dropdown with list of existing categories and no category with id=0. CategoryStoreRequest must check if the category_id from the form belongs to existing category or is 0
Something like that
public function rules() {
return [
"name" => "required|min:3",
"category_id" => "required|exists:categories,id,except_if_value_is_0"
];
}
What is the most elegant way to achieve it?
It turns out that nullable
is one quite elegant way to do it. When submitting the form then category_id array key is still present but its value is null. nullable allows the key to be null also.
public function rules() {
return [
"name" => "required|min:3",
"category_id" => "nullable|exists:categories,id"
];
}
In addition the select value must be ""
<select name="category_id">
<option value="">No category selection</option>
<option value="1">Cat 1</option>
</select>
you can create new validation to handle see this example: in your_project_name/app/providers/AppServicesProviders.php
Validator::extend(
'exists_or_null',
function ($attribute, $value, $parameters)
{
if($value == 0 || is_null($value)) {
return true;
} else {
$validator = Validator::make([$attribute => $value], [
$attribute => 'exists:' . implode(",", $parameters)
]);
return !$validator->fails();
}
}
);
in your example do that
public function rules() {
return [
"name" => "required|min:3",
"category_id" => "required|exists_or_null:categories,id"
];
}
You can use sometimes
. In this case, the rule will only be applied if a filled category_id is submitted.
public function rules() {
return [
"name" => "required|min:3",
"category_id" => "sometimes|exists:categories,id"
];
}
Change your html, so that there's no value set:
<select name="category_id">
<option value="">No category selection</option>
<option value="1">Cat 1</option>
</select>
Instead of checking for exists or 0
, you could set your custom zero value to NULL
or an empty string.
You need to change a little bit of your logic, but then you can validate it correctly by using the sometimes rule:
public function rules() {
return [
"name" => "required|min:3",
"category_id" => "sometimes|exists:categories,id"
];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With