Philipp Mochine
Philipp Mochine

Reputation: 4705

Laravel Validation: required_with or required_with_all condition always passing

A similar post was made here, but still it does not work in my case.

$v = \Validator::make($keys, [
            'overall' => 'required',
            'taste' => 'sometimes|required_with_all:freshness, quantity, value',
            'freshness' => 'sometimes|required_with_all:taste, quantity, value',
            'quantity' => 'sometimes|required_with_all:taste, freshness, value',
            'value' => 'sometimes|required_with_all: taste, quantity, freshness'
        ]);

dd($v->fails()); //-> false should be true

I'm trying to summarise what I'm doing:

I tried to do it with "required_with", but it doesn't work.

My example array is:

array:4 [
  "overall" => 0
  "freshness" => 1
  "quantity" => 2
  "value" => 3
]

With "taste" missing.

So what am I doing wrong?

If I understand the docs completely:

required_with_all:foo,bar,... The field under validation must be present only if all of the other specified fields are present.

That means if:

E.g. 'taste' => 'sometimes|required_with_all:freshness, quantity, value', means that when freshness, quantity, value are present, and param 'taste' is not present, then the validation error will occur.

But it does not...

Upvotes: 1

Views: 1090

Answers (1)

Saumini Navaratnam
Saumini Navaratnam

Reputation: 8850

sometimes : field will only be validated if it is present in the data array.

Since taste is not presented in your example array it will not validate. Try without sometimes

'taste' => 'required_with_all:freshness, quantity, value'

Upvotes: 1

Related Questions