Validation rules required_if with other condition (Laravel 5.4)
Asked Answered
J

5

18

I got a problem with validation rules with nested conditions.

class StoreRequest extends Request
{
        public function authorize(){
        return true;
        }

        public function rules(){
                return [
                    'type_id'     => 'required|integer',
                    'external_id' => 'required_if:type_id,==,3|integer',
                ];
        }
}

Indeed I want to : - check the external_id only if the type_id equal to 3 - and check if it's an integer.

When I post my form, the rules works when I select a type_id equal to 3. But if I select another type_id, like 1 or 2, the validation does not pass :

The external_id must be an integer.

I try to add the nullable condition but required_if does not work anymore

Did you have any idea ?

Jahdol answered 18/2, 2017 at 20:24 Comment(0)
H
14

Your rule performs two checks that are independent of one another; just because the external_id field is not required when the type_id != 3, does not mean the integer check is ignored.

What you are looking for is a conditional rule, which gives you finer control of when to perform a check, e.g. :

$validator = Validator::make($data, [
    'type_id'   => 'required|integer'
]);

$validator->sometimes('external_id', 'required|integer', function($input) {
    return $input->type_id == 3;
});

When using form validation, you can access the underlying validator instance by overriding the getValidatorInstance() method:

class StoreRequest extends Request
{
        public function authorize(){
        return true;
        }

        public function rules(){
                return [
                    'type_id'     => 'required|integer'
                ];
        }

        protected function getValidatorInstance() {
            $validator = parent::getValidatorInstance();
            $validator->sometimes('external_id', 'required|integer', function($input) {
                return $input->type_id == 3;
            });
            return $validator;
        }
}
Helfrich answered 18/2, 2017 at 21:41 Comment(0)
N
36

Just came across the same problem and found the following answer that seems to work for me:

issue-using-required-if-validation-rule-in-form-builder

     return [
                'type_id'     => 'required|integer',
                'external_id' => 'required_if:type_id,==,3|nullable|integer',
            ];

Result for me:

field not populated, type id not 3 - pass

field not populated, type id 3 - fail - required field

field populated, type id 3, non-integer - fail in integer rule

field populated, type id 3, integer - pass - all good!

note - think nullable rule came in Laravel 5.3

Nieshanieto answered 16/7, 2017 at 6:3 Comment(3)
The link that you provided is broken.Genagenappe
Works like a charm!Katricekatrina
@Nick A i know it works if the operation is = but i tried to use the > operator and it always says: required if value is 2. I tried: required_if:value, >, 2 Please help!Kandacekandahar
H
14

Your rule performs two checks that are independent of one another; just because the external_id field is not required when the type_id != 3, does not mean the integer check is ignored.

What you are looking for is a conditional rule, which gives you finer control of when to perform a check, e.g. :

$validator = Validator::make($data, [
    'type_id'   => 'required|integer'
]);

$validator->sometimes('external_id', 'required|integer', function($input) {
    return $input->type_id == 3;
});

When using form validation, you can access the underlying validator instance by overriding the getValidatorInstance() method:

class StoreRequest extends Request
{
        public function authorize(){
        return true;
        }

        public function rules(){
                return [
                    'type_id'     => 'required|integer'
                ];
        }

        protected function getValidatorInstance() {
            $validator = parent::getValidatorInstance();
            $validator->sometimes('external_id', 'required|integer', function($input) {
                return $input->type_id == 3;
            });
            return $validator;
        }
}
Helfrich answered 18/2, 2017 at 21:41 Comment(0)
A
0

try this pass the value directly refer: https://www.npmjs.com/package/validatorjs

"required_if:anotherfield,value"

The field under validation must be present and not empty if the another field field is equal to any value.

'type_id'     => 'required|integer',
'external_id' => 'required_if:type_i,3|integer',
Audy answered 12/2, 2021 at 13:39 Comment(0)
H
0

i think the easy way is

'type_id'     => 'required|integer',
'external_id' => $this->type_id === 3 ? 'required|integer' : 'nullable',
Hermia answered 12/9, 2023 at 5:53 Comment(0)
S
-2

try this,

  class StoreRequest extends Request
    {
            public function authorize(){
            return true;
            }

            public function rules(){
                    return [
                        'type_id'     => 'required|integer',
                        'external_id' => 'required_if:type_id|in:3|integer',
                    ];
            }
    }
Strap answered 19/2, 2017 at 4:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.