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

alangonzaga's avatar

Update request with multiple nested fields with unique rule, how to assign exception id to each validation field?

I have a system for my store (we buy and sell iPhones/Macbooks...[they have unique IMEIs and/or Serial Number]), there is a page for the purchases where I enter the order I made from my supplier, let's say I bought 10 iPhones: Take a look on the store method (it is fine, working properly - it is checking if the IMEI is not already on the DB)

$rules= [
            'fornecedor_id' => 'required|exists:fornecedores,id',
            'apples.*.produto_id' => 'required|exists:produtos,id',
            'apples.*.imei_1' => 'nullable|unique:apples,imei_1|unique:apples,imei_2|distinct',
            'apples.*.imei_2' => 'nullable|unique:apples,imei_1|unique:apples,imei_2|distinct',
            'apples.*.serial_number' => 'nullable|unique:apples,serial_number|distinct',
];

$request->validate($rules, $feedbacks);

The problem is when I need to make an edit on the purchase (update method), I need on each validation of an iPhone from the purchase to ignore the id line from DB of that iPhone specificaly (since it's a nested array of iphones), how can I pass to the validation rule to ignore this way?

On the request, I have the id of the iPhone to be ignored on each nested value:

+request: Symfony\Component\HttpFoundation\InputBag {#44 ▼
    #parameters: array:8 [▼
      "_token" => "pveIoqaU2lArLHnogRLvd5l4FAIphbwdewCLZSl1"
      "_method" => "PUT"
      "fornecedor_id" => "3"
      "numero_pedido" => "6512538817912"
      "transportadora_id" => "1"
      "rastreio" => "5825741093730"
      "observacoes" => "Quis recusandae nulla aut unde. Et autem earum quia corrupti at sed et. Praesentium debitis iste ex et. Ut libero sint ullam non numquam sit."
      "apples" => array:14 [▼
        0 => array:6 [▼
          "apple_id" => "2"
          "produto_id" => "5"
          "grade_id" => "1"
          "imei_1" => "3705372154470"
          "imei_2" => "6976872529328"
          "serial_number" => "6849463968816"
        ]
        1 => array:6 [▼
          "apple_id" => "12"
          "produto_id" => "7"
          "grade_id" => "2"
          "imei_1" => "6897769898955"
          "imei_2" => "3412771080902"
          "serial_number" => "5349011111622"
        ]
        2 => array:6 [▶]
        3 => array:6 [▶]
        4 => array:6 [▶]
        5 => array:6 [▶]
        6 => array:6 [▶]
        7 => array:6 [▶]
        8 => array:6 [▶]
        9 => array:6 [▶]
        10 => array:6 [▶]
        11 => array:6 [▶]
        12 => array:6 [▶]
        13 => array:6 [▶]
      ]
    ]
  }

I already tried some and didn't work:

'apples.*.imei_1' => "nullable|unique:apples,imei_1,apples.*.apple_id|unique:apples,imei_2,apples.*.apple_id|distinct

'apples.*.imei_1' => "nullable|unique:apples,imei_1|unique:apples,imei_2|distinct|exclude_if:id,apples.*.apple_id"

I came across some ideas, but I would like to find some more clean. The ideas I had:

-Instance Validator and make a foreach and validate each iPhone, sending the id to ignore on eachloop, and stopping the validate to redirect automatically, grouping the validation error messages, and then after validate all, check if has some error message and redirect back if has.

  • Send each iphone with the specific request part to the AppleController update method so there it checks the unicity and return error messages or no.
0 likes
1 reply
alangonzaga's avatar

I guess I found the solution:

$regras = [
            'fornecedor_id' => 'required|exists:fornecedores,id',
            'apples.*.produto_id' => 'required|exists:produtos,id',
            'apples.*.imei_1' => 'distinct',
            'apples.*.imei_2' => 'distinct',
            'apples.*.serial_number' => 'distinct',
        ];

        foreach ($request->get('apples') as $index => $apple) {
            $regras['apples.' . $index . '.imei_1'] = 'nullable|unique:apples,imei_1,'.$apple['apple_id'].'|unique:apples,imei_2,'.$apple['apple_id'].'|distinct';
            $regras['apples.' . $index . '.imei_2'] = 'nullable|unique:apples,imei_1,'.$apple['apple_id'].'|unique:apples,imei_2,'.$apple['apple_id'].'|distinct';
            $regras['apples.' . $index . '.serial_number'] = 'nullable|unique:apples,serial_number,'.$apple['apple_id'].'|distinct';
        }

Please or to participate in this conversation.