Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

murilo's avatar
Level 10

verify in array laravel request

hello , I have 2 fields .

  • product_list_ids - List of producs IDS ( Array )
  • prodict_selected_id - Product Selected ID ( Integer )

I need to verify in prodict_selected_id if contains in product_list_ids ( array ) .

I saw that has in laravel request in_array . I tryed to do like this -

	public function rules(): array
    {
		return  return ['product_list_ids' => 'required|array', 'prodict_selected_id' => 'required|in_array:product_list_ids'];
	}


but didnt work

0 likes
5 replies
LaryAI's avatar
Level 58

To solve the problem of verifying if prodict_selected_id is contained within product_list_ids in a Laravel request, you can use a custom validation rule. The built-in in_array rule in Laravel does not work directly with another field's array, so you need to create a custom rule for this purpose.

Here's how you can achieve this:

  1. Create a Custom Validation Rule:

First, create a custom validation rule using the Artisan command:

php artisan make:rule ProductSelectedInList

This will generate a new rule class in the app/Rules directory. Open the generated file and modify it as follows:

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class ProductSelectedInList implements Rule
{
    protected $productListIds;

    public function __construct($productListIds)
    {
        $this->productListIds = $productListIds;
    }

    public function passes($attribute, $value)
    {
        return in_array($value, $this->productListIds);
    }

    public function message()
    {
        return 'The selected product ID is not in the product list.';
    }
}
  1. Use the Custom Rule in Your Form Request:

Next, use this custom rule in your form request. Assuming you have a form request class, modify the rules method to use the custom rule:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\ProductSelectedInList;

class YourFormRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'product_list_ids' => 'required|array',
            'prodict_selected_id' => [
                'required',
                new ProductSelectedInList($this->input('product_list_ids'))
            ],
        ];
    }
}
  1. Ensure Correct Input Names:

Make sure that the input names in your request match exactly with the names used in the validation rules. In your case, it should be product_list_ids and prodict_selected_id.

  1. Handle Validation in Controller:

Finally, ensure that you are using this form request in your controller method:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\YourFormRequest;

class YourController extends Controller
{
    public function store(YourFormRequest $request)
    {
        // Your logic here
    }
}

With these steps, you should be able to validate that prodict_selected_id is contained within product_list_ids using a custom validation rule in Laravel.

tykus's avatar

I am not sure I understand the use-case here; why is product_list_ids coming from the user at all?

kevinbui's avatar

The validation rule should work. I notice that you got a typo for that field prodict_selected_id . Should that be product_selected_id? What is the actual field's name?

Pls make sure naming is correct and consistent.

MohamedTammam's avatar

Try that.

public function rules(): array
    {
		return [
			'product_list_ids' => 'required|array',
			'prodict_selected_id' => 'required|in_array:product_list_ids.*',
		];
	}
Snapey's avatar

You should not verify a field against a list that could have been tampered with by the user.

Please or to participate in this conversation.