Required_If Laravel 5 Validation

required_if Laravel 5 validation

assuming that list_type is the name of the select box to choose from (values : selling or rent)

use it this way

"sale_price" => "required_if:list_type,==,selling"

what does this mean?
:

the sale price is only required if the value of list_type is equal to selling

do the same for rent_price

edit

public function rules()
{
return [
'list_type' => 'required',
'sale_price' => 'required_if:list_type,==,For Sale',
'rent_price' => 'required_if:list_type,==,For Rent'
}

required_if validation rule with the array validation rule is not working | Laravel | Validation

Use the exclude_if rule to conditionally exclude the field from validation.

$data = [
"name" => "testing",
"type" => "public",
"streaming_platforms" => [
["id" => 16, "platform" => "youtube", "enable" => true, "stream_url" => "https://www.google.com", "stream_key" => "abdefghijk"],
["id" => 17, "platform" => "facebook", "enable" => false, "stream_url" => null, "stream_key" => null],
["id" => 18, "platform" => "custom", "enable" => false, "stream_url" => null, "stream_key" => null],
]
];

$rules = [
'name' => 'required|string|min:3|max:255',
'type' => 'required|in:private,public',
'streaming_platforms.*.stream_url' => 'exclude_if:streaming_platforms.*.enable,false|required|url',
'streaming_platforms.*.stream_key' => 'exclude_if:streaming_platforms.*.enable,false|required|string',
];

$messages = [
'streaming_platforms.*.stream_url.required' => 'The stream url field is required when platform is enable.',
'streaming_platforms.*.stream_key.required' => 'The stream key field is required when platform is enable.',
];

$v = Illuminate\Support\Facades\Validator::make($data, $rules, $messages);

dump($v->fails());

// false

Note this also prevents the data from being output with the Illuminate\Validation\Validator::validated() method:

dump($v->validated());

Output:

   [
"name" => "testing",
"type" => "public",
"streaming_platforms" => [
[
"stream_url" => "https://www.google.com",
"stream_key" => "abdefghijk",
],
],
]

Laravel request validation required_if with multiple conditions

I found a solution that can be applied for the request validation, it's more simple to use with the current implementation.

return request()->validate([
'sets.*.attribute' => function ($attr, $value, $fail) {
if (request()->input('type') == 'image' && !request()->input('sets')[explode('.', $attr)[1]]['isUsed'] && empty($value)) {
$fail('The ' . $attr . ' is required.');
}
},
]);

required_if not case in Laravel

You can try required_unless.

 'expires_at' => 'required_unless:target,ads',

Here is the documentation for that rule.

required if in Laravel

You don't need the whole expression, the correct format is as follows:

 return [
'game_id' => 'required',
'platform_id' => 'required_if:columnName:value',
];

How to set required_if in laravel 7

$validator = Validator::make($request->all(), [
'color_name' => 'required_with:color_code',
]);

if ($validator->fails()) {
return $validator->errors()->first();
}

Use this to solve the problem.

For array please re shape your array into this

array:2 [
0 => array:2 [
"color_name" => "red"
"color_code" => "#77"
]
1 => array:2 [
"color_name" => "blue"
"color_code" => "#88"
]
]

$validator = Validator::make($request->all(), [
'color'=>'array',
'color.*.color_name' => 'required_with:color.*.color_code',
]);


Related Topics



Leave a reply



Submit