Framework: Validation rule required_if doesn't work for boolean values

Created on 27 Mar 2015  路  12Comments  路  Source: laravel/framework

This fails:

Validator::make([
    'whole_day' => true,
    'time' => ''
], [
    'time' => 'required_if:whole_day,false'
]);

Most helpful comment

It seems to work with 'required_if:whole_day,0'. Thanks. It should be documented though.

All 12 comments

Hey @GrahamCampbell can you please provide some information on the issue? Is it somehow an intended behavior?

Not sure. Have you tried with 1 or "true"?

It seems to work with 'required_if:whole_day,0'. Thanks. It should be documented though.

:)

Just had exactly the same problem, still not documented. Should definitely be either fix for documented.

+1 Thanks @pdcmoreira
It should be documented

Perhaps we can have such that required_if looks for casts to boolean and then checks for true/false instead of just 1/0?
protected $casts = ['whole_day' => 'boolean'];
required_if:whole_day,true.

A reason is the default validation message that appears:
The COLUMN_NAME field is required when whole day is true
definitely looks better than:
The COLUMN_NAME field is required when whole day is 1.

any news?

It also just doesn't work if incoming value type is actual boolean.

It also just doesn't work if incoming value type is actual boolean.

My hack is, to refactor this line,

'citizen'           => 'required|boolean',

to

'citizen'           => 'required',

So, now, it will be available

            'passport_number'   => 'required_if:citizen,true',
            'visa_type'         => 'required_if:citizen,false',

Why is this closed? Either the implementation should be fixed, or the documentation should reflect this surprising (in an unwelcome sense) behavior.

Anyone's free to send in prs to the docs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

iivanov2 picture iivanov2  路  3Comments

JamborJan picture JamborJan  路  3Comments