Laravel 10: implementing Custom Validation Rules for Dynamic Form Fields
I've been working on this all day and I'm working through a tutorial and I'm having a hard time understanding I'm refactoring my project and I'm currently working with an scenario with implementing custom validation rules for dynamic form fields in Laravel 10..... I have a form that allows users to add multiple entries dynamically, where each entry must have its own validation context. For instance, users can input a series of 'tags' for a post, and I want to ensure that each tag is unique within that post. However, I'm struggling to set up the validation rules correctly, particularly for the dynamic nature of the fields. Here's what I have so far: ```php public function store(Request $request) { $tags = $request->input('tags'); $validator = Validator::make($request->all(), [ 'tags.*' => ['required', 'string', 'unique:tags,name'], // unique validation not working as expected ]); if ($validator->fails()) { return back()->withErrors($validator)->withInput(); } // Save the tags logic here } ``` When I submit the form with duplicate tags, it doesn't seem to trigger the unique validation correctly. The behavior I'm getting is: ``` The tags.0 field must be unique. ``` However, this message appears even when all tags are unique in the context of the post. I've also tried using `unique:tags,name,NULL,id` to bypass the current post's tags but to no avail. I’ve looked into form request validation but couldn’t find a way to implement it dynamically for such cases. Can anyone suggest how I could properly implement this validation for dynamically added fields? Any pointers on structuring this or alternatives would be greatly appreciated! Any pointers in the right direction? For context: I'm using Php on Ubuntu 22.04. Could this be a known issue? This is for a web app running on Windows 11. My team is using Php for this mobile app. Thanks for any help you can provide!