Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Livewire how to validate array data

I try to validate array when form submitted as

<input type="text" class="form-control" wire:model.lazy="data.name" placeholder="name">
    //at livewire component class
    $data = [
        'name' => 'someValue',
        'phone' => 'someValue',
        'email' => 'someValue'
    ]

I try this

    Validator::make($this->data,[
      'name' => 'required',
       ...
    ])->validate();

but not working, please help me.

like image 832
Joseph Prosper Avatar asked Dec 06 '25 06:12

Joseph Prosper


2 Answers

You can validate using the same syntax as you have used in wire:model:

$this->validate([
    'data.name' => ['required'],
]);
like image 121
Dan Harrin Avatar answered Dec 07 '25 20:12

Dan Harrin


I'm new using Livewire, Idk if this related to your question. But, since your question is on top in Google. Maybe this will help other people.

public function saveAdd()
{
  $rules_state = [
    'state_name' => 'required',
    'state_email' => 'required',
  ];
  $check_state = $this->inputsFormAddProduct;
  foreach ($this->inputsFormAddProduct as $key => $value) {
    $rules_state = array_merge($rules_state, [
      'state_product.'.$value => 'required',
      'state_qty.'.$value => 'required',
    ]);
  }
  $validatedData = $this->validate($rules_state,
    [
        'required' => 'The :attribute cannot be empty.',
    ],
  );
  dd($rules_state);
}
like image 22
No Name Avatar answered Dec 07 '25 20:12

No Name