0

I send to Laravel this JSON data:

[
  {"name":"...", "description": "..."},
  {"name":"...", "description": "..."}
]

I have a StoreRequest class extends FormRequest:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name' => 'required|string|min:1|max:255',
            'description' => 'nullable|string|max:65535'
        ];
    }
}

In my controller I have this code, but it doesn't work with array:

    public function import(StoreRequest $request) {
        $item = MyModel::create($request);

        return Response::HTTP_OK;
    }

I found this solution to handle arrays in the Request rules():

    public function rules()
    {
        return [
            'name' => 'required|string|min:1|max:255',
            'name.*' => 'required|string|min:1|max:255',
            'description' => 'nullable|string|max:65535'
            'description.*' => 'nullable|string|max:65535'
        ];
    }

How can I update the StoreRequest and/or the import() code to avoide duplicate lines in rules()?

11
  • Possible duplicate of How to write validation rule for JSON laravel? Commented Aug 27, 2019 at 12:14
  • Yeah, I'm sorry. I overlooked the [] and was confused. Please have a look at the linked question and my answer there. Commented Aug 27, 2019 at 12:14
  • @Namoshek: I think this isn't a duplicate... In this linked site they use $validator = Validator::make() what I don't use, because I have a StoreRequest class. Or if this is a same thing, then I dont understand how can I use this two together. Commented Aug 27, 2019 at 12:17
  • The rules returned by the rules() method are used to create a Validator as discussed in the other question. It is the same thing, just an abstraction of it... Commented Aug 27, 2019 at 12:20
  • Basically, form request classes (like your StoreRequest) are used to extract logic from the controller. You can do exactly the same stuff also with a normal Request object and the Validator in your controller. It is still recommended to use form requests in order to keep your controller small and simple. Commented Aug 27, 2019 at 12:22

1 Answer 1

1

As you have an array of data you need to put * first:

public function rules()
{
   return [
       '*.name' => 'required|string|min:1|max:255',
       '*.description' => 'nullable|string|max:65535',
   ];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Will this working with a single (non array) objects too?
No, this would require the rules without *.. You can't do both in one ruleset without duplication. IMHO the best way would be to always send an array or use two different FormRequests as they are doing two different things (creating one record vs. creating many records).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.