prgrm prgrm - 1 year ago 106
PHP Question

Validating array request in Laravel

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download