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

betaversion's avatar

required_without => Validation for an array field

Hi guys, I have the following request input in Laravel:

{
  "date": "2022-07-07",
  "remarks": "My First Transaction !",
  "transactions": [
    {
      "item": 1,
      "in": "54000"
    },
    {
      "item": 2,
      "out": "54000"
    }
  ]
}

I want to validate the array transactions where one of the two field "in" or "out" is required ! So far I got to:

 $rules = [
            'date' => 'required|date',
            'remarks' => 'required|string',
            'transactions.*.item' => 'required|integer',
			'transactions.*.in' =>'required_without:transactions.*.out',
			'transactions.*.out' =>'required_without:transactions.*.in'
        ];

I am getting the following error: Undefined array key "out""

Please Let me know what am i missing ... !!!

0 likes
3 replies
Sinnbeck's avatar

I would write a custom validation for this. Sounds like quite specific. Or change the format to not use an array in this way

transaction.in' =>
1 like
betaversion's avatar

@Sinnbeck Thank you ! Can you suggest me how to create custom validation for the case.... I am confused !! How to pass the two attribute to the validate rule ... ??

Please or to participate in this conversation.