Required field only if another field has a value, must be empty otherwise
Asked Answered
O

4

9

My question is about Laravel validation rules.

I have two inputs a and b. a is a select input with three possible values: x, y and z. I want to write this rule:

b must have a value only if a values is x. And b must be empty otherwise.

Is there a way to write such a rule? I tried required_with, required_without but it seems it can not cover my case.

In other words, if the previous explanation was not clear enough:

  • If a == x, b must have a value.
  • If a != x, b must be empty.
Open answered 24/1, 2018 at 10:55 Comment(0)
O
4

You have to create your own validation rule.

Edit app/Providers/AppServiceProvider.php and add this validation rule to the boot method:

// Extends the validator
\Validator::extendImplicit(
    'empty_if',
    function ($attribute, $value, $parameters, $validator) {
        $data = request()->input($parameters[0]);
        $parameters_values = array_slice($parameters, 1);
        foreach ($parameters_values as $parameter_value) {
            if ($data == $parameter_value && !empty($value)) {
                return false;
            }
        }
        return true;
    });

// (optional) Display error replacement
\Validator::replacer(
    'empty_if',
    function ($message, $attribute, $rule, $parameters) {
        return str_replace(
            [':other', ':value'], 
            [$parameters[0], request()->input($parameters[0])], 
            $message
        );
    });

(optional) Create a message for error in resources/lang/en/validation.php:

'empty_if' => 'The :attribute field must be empty when :other is :value.',

Then use this rule in a controller (with require_if to respect both rules of the original post):

$attributes = request()->validate([
    'a' => 'required',
    'b' => 'required_if:a,x|empty_if:a,y,z'
]);

It works!

Side note: I will maybe create a package for this need with empty_if and empty_unless and post the link here

Open answered 24/1, 2018 at 14:3 Comment(0)
D
3

Required if

required_if:anotherfield,value,... The field under validation must be present and not empty if the anotherfield field is equal to any value.

'b' => 'required_if:a,x'
Disillusionize answered 24/1, 2018 at 11:1 Comment(4)
Thanks, but unless I mistake, it covers only the first rule: "If a == x, b must have a value.". But it does not cover the second rule: "If a != x, b must be empty."Open
@Open you need to create a custom rule for this.Haunch
@AlexeyMezenin OK, if you want you can post it as an answer. Otherwise, I will post my own. So as you want! :)Open
@Open please post it. )Haunch
F
1

In Laravel 8+ check if prohibited and prohibited_if validators work for this:

'b' => 'required_if:a,x|prohibited_if:a,!x'
Floranceflore answered 5/11, 2022 at 4:24 Comment(0)
G
0

Laravel has its own method for requiring a field when another field is present. The method is named required_with.

Gimmick answered 26/10, 2020 at 18:4 Comment(1)
the author of the question has mentioned, " I tried required_with, required_without but it seems it can not cover my case." then why are you suggesting the sam thingSaltatorial

© 2022 - 2024 — McMap. All rights reserved.