Framework: [5.4] Validation rule required_if not working properly

Created on 17 Feb 2017  路  11Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.11
  • PHP Version: 7.0.15
  • Database Driver & Version:

Description:

Validation rule required_if doesn't seem to get to combine with other rules. For example I have:
'company_org_number' => 'required_if:is_corporate,1|regex:/^[0-9\-]+$/'

It's the same for example this rule:

'occupation_to_month' => 'required_if:occupation,finite_time|numeric'

Even if is_corporate is 0 it still gives me error for the regex. This has been working in all Laravel versions, but not since the upgrade to Laravel 5.4. And it is not documented

Steps To Reproduce:

Create a validation rule required_if combined with another rule

Most helpful comment

I had the same issue. I added 'nullable' to the fields and it worked.

All 11 comments

But this doesn't apply to the validate request in controller:
$this->validate($request, [ 'email' => 'required|email', 'company_org_number' => 'required_if:is_corporate,1|regex:/^[0-9\-]+$/' ]);

validate method inside ValidatesRequests.php

/** * Validate the given request with the given rules. * * @param \Illuminate\Http\Request $request * @param array $rules * @param array $messages * @param array $customAttributes * @return void */ public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = []) { $validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes); if ($validator->fails()) { $this->throwValidationException($request, $validator); } }

And also, why has this been working for me throughout Laravel 5.1, 5.2, 5.3 until now if that is the case

I also had similar issue. My code was working in earlier Laravel versions. But in 5.4 it's not.
@themsaid told me that it is due to ConvertEmptyStringsToNull middleware in l5.4. When I disabled tht middleware it worked. But disabling the middleware is not the right solution.
Here is the link to my issue https://github.com/laravel/framework/issues/17817
They also closed my issue. .

Drop the ConvertEmptyStringsToNull middleware if you want the behaviour in 5.3, the middleware converts empty strings to null, the validator skips empty strings but not null values.

Check: http://themsaid.com/laravel-convert-empty-strings-to-null-20170130/

I had the same issue. I added 'nullable' to the fields and it worked.

thanks everyone for helping me solve this one....was scratching the old noggin for a while there...

I had the same issue. I added 'nullable' to the fields and it worked.

works fine!

I had the same issue. I added 'nullable' to the fields and it worked.

this should be in docs

That's expected, please read http://themsaid.com/laravel-advanced-validation-conditionally-adding-rules-20170110/

Link not found

https://web.archive.org/web/20170602164216/https://themsaid.com/laravel-advanced-validation-conditionally-adding-rules-20170110/

Was this page helpful?
0 / 5 - 0 ratings