Joseph Prosper
Joseph Prosper

Reputation: 72

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.

Upvotes: 2

Views: 9123

Answers (2)

No Name
No Name

Reputation: 11

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);
}

Upvotes: 1

Dan Harrin
Dan Harrin

Reputation: 957

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

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

Upvotes: 9

Related Questions