The issue you're facing is due to the fact that Laravel automatically decodes JSON input to arrays. So when you're trying to validate it as a JSON string, it fails because it's already an array.
You can solve this by creating a custom validation rule. Here's how you can do it:
First, create a new rule using Artisan command:
php artisan make:rule IsValidJson
This will create a new rule in app/Rules directory. Open the IsValidJson.php file and modify the passes method like this:
public function passes($attribute, $value)
{
return is_array($value) || is_object(json_decode($value));
}
This rule will pass if the value is an array (which means Laravel has already decoded it) or if it's a valid JSON string.
Then, in your FormRequest, you can use this rule like this:
use App\Rules\IsValidJson;
public function rules()
{
return [
'sections_details' => ['required', new IsValidJson],
];
}
This way, your validation will pass whether the JSON is already decoded or not.