Search code examples
phplaravellaravel-livewire

Laravel 9 required_if not validating


I'm using Laravel 9 and Livewire 2.0

I have an integer field called 'new_weight' that should validate required if the boolean checkbox 'stripped' is selected. Although 'stripped' is not selected, it's still validating as required. The validation $rules are being hit because if I remove the rule, it doesn't validate at all.

I also recently noticed that if I die and dump '$this->stripped' when the checkbox is selected, it dumps 'true' to the console and not '1', but if I leave unchecked, it dumps '0'--not sure if this matters.

edit.php

...
protected $rules = [
    'car_color' => 'required|string',
    'new_weight' => 'required_if:stripped,accepted|integer|min:1|max:999',
];


protected $messages = [
    'car_color' => 'Please enter car color.',
    'new_weight' => 'New Weight Value must be great than 1 but less than 999'
];
...

edit.blade.php

...
<div class="flex items-center h-5">
   <input wire:model.defer="stripped" id="stripped" name="stripped"
          wire:click="updateStripped"
          type="checkbox">
</div>
<div>
<input wire:model.defer="new_weight" id="new_weight" name="new_weight"
       type="text"
       placeholder="New Vehicle Weight in lbs:">
</div>
...

Solution

  • The new_weight field had a default null value in the database. Once I re-migrated with a default empty value, my problem was solved.