Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

html - Validating multiple checkboxes with multiple input text field

Hi there I am trying to validate multiple checkboxes with multiple input filed. I have a loop on checkboxes I want to validate if a checkbox is checked the input field is required and if checkbox is not checked then the input field is not required. input fields are not validating. if I remove [] from subscription_id its only validated the first input field

This is my from field.

 <div class="form-group">
                        <label>Subscriptions</label> 
                        @foreach ($subscriptions as $subscription)
                        <div class="form-check">
                            <label class="form-check-label"> 
                                <input type="checkbox" checked="" name="subscription_id[]" class="form-check-input" value="{{$subscription->id}}">
                                {{$subscription->name_en}}
                                 <i class="input-frame"></i></label>         
                                </div>
                                <div class="form-group">
                                    <label>Price</label> 
                                    <input type="number" class="form-control"   placeholder="Price" name="price[]" >
                                </div>
                        @endforeach
                    </div>

Validation Rules:

public function rules()
    {
        return [
            'name_en' => 'required|unique:packages,name_en',
            'name_ar' => 'required|unique:packages,name_ar',
            'deliveries' => 'required|numeric|gt:0',
            'subscription_id.*' => 'sometimes',
            'price' => 'required_with:subscription_id,on',
          
        ];
    }

Subscription Model

   public function packages()
    {
        return $this->belongsToMany(Package::class)->withPivot('price')->withTimestamps();
    }

Package Model

public function subscriptions()
    {
        return $this->belongsToMany(Subscription::class)->withPivot('price')->withTimestamps();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...