Validating array request in Laravel

prgrm

I have a dynamically generated form. I use an array to retrieve all the data. Something like this:

<input type="text" class="dynamically-generated" name="ItemArray[]">
<!-- This code as many times as desired -->

Now I want these inputs to be validated in the request:

public function rules()
{
    return [
     'Item' => 'integer'
    ];
}

I need however to do this in each of the elements in the array. This would be pretty easy in plain PHP. How is this possible in Laravel? I want to do this properly

andrew-caulfield

You're more than likely going to validate these inputs before storing them. So you could something like the following.

/**
 * Store a new something.
 *
 * @param  Request  $request
 * @return Response
 */
public function store(Request $request)
{
    $this->validate($request, [
        'item' => 'required|max:255'
    ]);

    // The something is valid, store in database...
}

The one you are using above is for complex validation scenarios.

You can read more on Validation in Laravel here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related