44

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

Rafael Korbas
  • 2,213
  • 3
  • 19
  • 30
  • Skip to the next section in the documentation after the one you linked to maybe? “Custom Validation Rules” - you can write your own custom validation logic if needed. – CBroe Apr 26 '17 at 13:21
  • ok, that would be one solution, but I see it as too cumbersome since you have to create a custom validator by modifying the Validator Facade, if I'm not missing anything. I see this requirement as quite a basic one, so I hope for an easier solution, or I'm just missing how to write the custom validation logic nicely, maybe you could give me a code snippet explaining how would you do it in this specific case, ideally by extending the existing "required" validation rule. – Rafael Korbas Apr 26 '17 at 13:25
  • Maybe use $this->validate($request, [ 'items' => 'array', ]; – Vahe Galstyan Apr 26 '17 at 13:37
  • @VaheGalstyan that does not work in my case because it does not require the presence of the field "items", it just expects an array, i.e. an object without the key "items" would be a valid input, which is wrong. – Rafael Korbas Apr 26 '17 at 13:41

7 Answers7

73

Try this:

public function createSomeResource(Request $request)
{
    $this->validate($request, [
        'items' => 'present|array',
    ];
    ...
}
silentavt
  • 756
  • 5
  • 4
  • Yes, I tried it and it almost does what I want, but an empty string still passes that validation rule as well (you can try it in artisan tinker) and I don't know how to fix it using the existing rules :/ I've already opened an issue on the github repository of Laravel: https://github.com/laravel/framework/issues/18948 – Rafael Korbas Apr 28 '17 at 15:35
  • 2
    @RafaelK. it looks like that above issue is resolved by using the `ConvertEmptyStringsToNull` middleware. I had the same problem as you and the [present](https://laravel.com/docs/5.4/validation#rule-present) validation modifier worked beautifully. Please accept the answer if it worked for you! – Stetzon Jul 19 '17 at 19:27
  • Thanks, worked perfectly in combination with `ConvertEmptyStringsToNull` – fgilio May 20 '19 at 18:39
21

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.

ImLeo
  • 991
  • 10
  • 17
8

1) Array is required

2) Value of all array is not null or empty

public static $rules = [
   'category' => 'required|array|min:1',
   'category.*' => 'required',
];
Parth kharecha
  • 6,135
  • 4
  • 25
  • 41
3

probably this should work

public function rules()
{
   return [
    "items"    => "required|array|min:0",
    "items.*"  => "required|string|distinct|min:0",
  ];
}
Sumit Kumar
  • 1,855
  • 19
  • 19
2

Here we go buddy...

public function createSomeResource(Request $request)
{
    $validate_us_pls = [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ];


    if( !empty($request->get('items')) ){
        $validate_us_pls['items'] = 'required';
    }

    $this->validate($request, $validate_us_pls);

}
Kabelo2ka
  • 419
  • 4
  • 14
2

Maybe this will be usefull?

size in array uses count

 'ids'=>'present|array|size:1'

or this

'users' => 'required|array|between:2,4'
1

Your problem is because you are not sending the request as JSON. Make sure that you have the settings on POSTMAN to send your request as JSON.

Roberto Lyra
  • 61
  • 1
  • 2