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

todstoychev's avatar
[
'users' => [
        [
                'name' => 'username1',
                'email' => 'email1',
        ],
        [
                'name' => 'username2',
                'email' => 'email2',
        ]
    ]
];

My way: In the request class at the roles() method:

for ($i = 0; $i < count($array['users']); $i++;) {
    $rules['users.' . $i . '.email'] = 'required|email';
    $rules['users.' . $i . '.name'] = 'required';
}
bcipollini's avatar

@Leon said:

I don't have access to try it currently, but could you use a wildcard with the dot notation?

    'email-address.*' => 'email'

I tried something like this, but the rule didn't seem to apply. Has anybody else tried this? This is the ideal solution to me.

shez1983's avatar

@bcipollini did you figure it out? i want to use something exactly like that?

in addition i want to say that the user should provide at least 4 emails..

ka.hazem's avatar

guys any news about this :(

am stuck in that issue for two days :/

Anton161's avatar

Hi. I need validate array values and array keys too. i create like this

public function rules()
    {

        ..........

        foreach($this->request->get('tickets') as $key => $val)
        {
          $rules['tickets.'.$key] = 'required|integer';
          $this->request->add(['ticket_id'.$key => $key]);
          $rules['ticket_id'.$key] = 'required|exists:tickets,id';
        }

        return $rules;
    }

For values its work, but for keys i recive required error

dump request parametrs looks

"tickets" => array:2 [
        1 => "1"
        2 => "0"
      ]
      "ticket_id1" => 1
      "ticket_id2" => 2
Anton161's avatar

I find solution i change attributes on all() method

public function all()
    {
        $attributes = parent::all();
        
       ...................

        return $attributes;

    }

this work. But why if i change attributest in rules() like this

$data = $this->request->all();
......
$this->request->replace($data);

this change all i need but again i have validation error that parametrs i change required

roerjo's avatar

The following is working fine for me when dealing with arrays in 5.4:

$rules = [
    'slides' => 'required|array',
    'slides.*.name' => 'required',
    'slides.*.resources.*.uuid' =>          
        'required|exists:resources,uuid'
];

By specifying the array rule on 'slides' I ensure I get at least one slide object in the array. The * wildcard is working fine for looping through multiple slides, and I can pass a custom column name when dealing with database rules.

This works for when my request is formatted like so:

{
    'slides': [
        {
            'name': 'slide1',
            'resources': [
                {
                    'uuid': 'blahblah',
                }, // repeat as necessary
            ]
        }, // repeat as necessary
    ]
}
Previous

Please or to participate in this conversation.