Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

388
Visualizações
Laravel 5 - validate array as required, but allow an empty array to be passed

I'm validating a request in Laravel 5.4 with the validator, see the documentation: https://laravel.com/docs/5.4/validation#validating-arrays

Basically, it's this code in the Controller:

public function createSomeResource(Request $request)
{
    $this->validate($request, [
        'items' => 'required',
    ];
    ...
}

I would like to require the presence of the field "items" and this code does it, but the problem is that the validation fails when the "items" field is an empty array, i.e.

{
    "fields": []
}

, which is an undesired behavior. I know that's the documented behavior of the "required" parameter but I don't see any "clean" workaround. I tried also:

public function createSomeResource(Request $request)
{
    $this->validate($request, [
        'items' => 'required_unless:items,[]',
    ];
    ...
}

but it fails as well, probably because the documentation says that it works with a different field after the "required_unless" clause, but I'm not totally sure about it.

Could you suggest me a way to require the presence of the field "items" without forbidding the empty array?

EDIT: another "obvious" approach that has come to my mind is to use the "present|array" rule and it almost does what I want, but unfortunately, an empty string passes that validation rule as well, which is maybe a bug in Laravel, maybe not - I opened an issue for it on the Laravel github repository: https://github.com/laravel/framework/issues/18948

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Try this:

public function createSomeResource(Request $request)
{
    $this->validate($request, [
        'items' => 'present|array',
    ];
    ...
}
about 4 years ago · Santiago Trujillo Relatório

0

Try:

public function createSomeResource(Request $request)
{
    $this->validate($request, [
        'items' => 'required|array|min:1',
    ];
    ...
}

From Laravel doc:

min:value The field under validation must have a minimum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.

about 4 years ago · Santiago Trujillo Relatório

0

1) Array is required

2) Value of all array is not null or empty

public static $rules = [
   'category' => 'required|array|min:1',
   'category.*' => 'required',
];
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda