如何在laravel livewire中验证多个数组表单字段

yqkkidmi  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(154)

我在livewire中使用的动态表单具有多个字段,具有相同的名称/型号,并且这些字段将在单击按钮后添加/复制或删除,我想提交所有这些数据以及验证。
目前,我试图验证并获取此数据,模型名称在末尾添加星号,如'name. '=>'required',但它不起作用。
Livewire类:
* 预订**

class BookingCreate extends Component
{
    public $form_count = 1;
    public $name = [];
    public $email = [];
    public $mobile = [];
    public $pan = [];
    public $pan_image = [];

public function addForm()
{
    $this->form_count = $this->form_count+1;
}

public function saveData()
{
    $this->resetErrorBag();
    
    $this->validate([
                'name.*' => 'required',
                'email.*' => 'required',
                'mobile' => 'required',
                'pan.*' => 'required',
                'pan_image.*' => 'nullable|mimes:png,jpg,jpeg,pdf',
            ],
            [
                'name.required' => 'First Owner field is required',
                'email.required' => 'First Owner Email field is required',
                'mobile.required' => 'Mobile number field is required',
                'pan.required' => 'Pan field is required',
                'pan_image.required' => 'Pan image is required',
            ]);
    dd($this->name);

    $booking = Booking::updateOrCreate([
                'name' => $this->name,
                'email' => $this->email,
                'mobile' => $this->mobile,
                'pan' => $this->pan,
                'pan_image' => $this->image_name
            ]);
}

这里dd($this-> name)在name模型中没有显示任何内容。
Livewire刀片:booking.blade.php

@for ($i = 1; $i <= $form_count; $i++)
    <div class="col-lg-11 col-md-12 col-sm-12">
        <div class="row">
            <div class="mb-3 col-sm-12 col-md-4 col-lg-4">
                <label>Name<span class="text-red">*</span></label>
                <input type="text" wire:model.defer="name" class="form-control display-inline mb-2" >
                @error('name')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="mb-3 col-sm-12 col-md-4 col-lg-4">
                <label>Email<span class="text-red">*</span></label>
                <input type="email" wire:model.defer="email" class="form-control display-inline mb-2">
                @error('email')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="mb-3 col-sm-12 col-md-4 col-lg-4">
                <label>Pan card No<span class="text-red">*</span></label>
                <input type="text" wire:model.defer="pan" class="form-control mb-2">
                @error('pan')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="mb-3 col-sm-12 col-md-4 col-lg-4">
                <label>Pan Card Attachment<span class="text-red">*</span></label>
                <div class="input-group">
                    <div class="custom-file">
                        <input type="file" wire:model.defer="pan_image" class="custom-file-input">
                    </div>
                </div>
                @error('pan_image')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
        </div>
    </div>
@endfor

<div class="col-lg-1 col-md-12 col-sm-12 d-flex align-items-center justify-content-center">
     <button wire:click="addForm" class="btn btn-primary d-block me-1" > + </button>
</div>

在Blade中,当我尝试在数组中创建模型时,比如wire:model.defer ="name []",它会给出错误。请提供解决方案。

ltskdhd1

ltskdhd11#

在你的类中,你不需要在数组中声明所有的属性变量,只要保持它的public $name即可。你需要迭代你的变量来一个接一个地插入所有变量,这样你的类就像这样。

class BookingCreate extends Component
{
    public $name;

public function saveData()
{
     //Your validation looks ok.

        for($i=1; $i<=count($this->name); $i++)
        {
            $booking = Booking::updateOrCreate([
                'name' => $this->name[$i],
            ]);
        }
}

在livewire的blade文件中,在迭代动态表单wire:models时,需要将索引id传递给模型名,这样它看起来就像

<div class="mb-3 col-sm-12 col-md-4 col-lg-4">
                <label>Name<span class="text-red">*</span></label>
                <input type="text" wire:model.defer="name.{{$i}}" class="form-control display-inline mb-2" >
                @error('name.{{$i}}')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>

相关问题