CodexBloom - Programming Q&A Platform

Laravel 9: Difficulty with Validation Rules for Nested Request Data in Form Requests

👀 Views: 1 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-11
laravel validation form-requests PHP

I'm trying to figure out I've been working on this all day and I'm following best practices but I'm trying to configure I've spent hours debugging this and I've looked through the documentation and I'm still confused about I'm having trouble validating nested data in a Form Request in Laravel 9. I have a form where users can provide multiple addresses, and I'm using a nested structure in my request. Here's the structure I'm working with: ```json { "name": "John Doe", "email": "john@example.com", "addresses": [ { "street": "123 Main St", "city": "Anytown" }, { "street": "456 Elm St", "city": "Othertown" } ] } ``` In my `FormRequest`, I have set up the validation rules like this: ```php public function rules() { return [ 'name' => 'required|string|max:255', 'email' => 'required|email|unique:users,email', 'addresses.*.street' => 'required|string|max:255', 'addresses.*.city' => 'required|string|max:255', ]; } ``` However, when I submit the form, I'm getting the following behavior message: ``` The given data was invalid. ``` After reviewing the request data in the HTTP debugger, I noticed that the `addresses` array is recognized, but the nested rules for `street` and `city` don't seem to apply correctly. I've tried returning the `validated()` data in the `FormRequest` to debug it, and it looks like Laravel is not recognizing the array structure properly. I've also checked to ensure that my request is being sent as `application/json`, and I've set up the correct middleware to handle JSON requests. Is there something I'm missing in my setup? Any help would be greatly appreciated! Has anyone else encountered this? I'd be grateful for any help. For context: I'm using Php on CentOS. What are your experiences with this? I'm working on a REST API that needs to handle this. Any advice would be much appreciated.