Okan K
Okan K

Reputation: 51

Laravel Livewire Validation With Database Column

I have a projects table contains contract column. contract column is nullable. Because some projects may not have contract. If a project have contract, contract must be accepted:

// Blade 
@if($project->contract)
<div>
    <div class="inline-flex items-center space-x-2">
        <input wire:model.defer="contract" id="contract" type="checkbox">
        <label for="contract">I accept <a href="#">contract</a></label>
        @error('contract')
            <span class="text-xs text-red-500">{{ $message }}</span>
        @enderror
    </div>
</div>
@endif


// Component
$this->validate([
        'contract' => 'accepted',
    ],

// Tried like this too
$this->validate([
        'contract' => 'exists:projects|accepted',
    ],

I tried theese but null contract tables are validating too. How can I validate this null columns?

Upvotes: 0

Views: 661

Answers (1)

Tanvir Ul Haque
Tanvir Ul Haque

Reputation: 185

write this

if(isset(request('contract'))){
     $this->validate([
                'contract' => 'exists:projects|accepted',
            ]
  }

Upvotes: 1

Related Questions