It seems like you want to validate fields individually as they are updated, rather than validating all fields at once. In Livewire, you can achieve this by specifying the field you want to validate in the validate method within the updated hook. Here's how you can modify your updated method to validate individual fields:
public function updated($propertyName)
{
$this->validateOnly($propertyName);
}
The validateOnly method will only validate the field that has been updated, based on the rules you've defined in your rules method. This way, you won't get validation errors for fields that haven't been touched yet.
Here's how your Livewire component might look with the updated method:
use Livewire\Component;
use Illuminate\Validation\Rule;
class YourComponent extends Component
{
public string $name = '';
public ?string $begin = null;
public ?string $end = null;
public function rules()
{
return [
'name' => 'required|min:4|max:255|alpha_num',
'begin' => 'sometimes|date|after_or_equal:today',
'end' => 'sometimes|date|after_or_equal:begin',
];
}
public function updated($propertyName)
{
$this->validateOnly($propertyName);
}
// Other methods...
}
Please note that I've corrected the validation rule for name by removing :ascii from the alpha_num rule, as alpha_num does not take any parameters. Also, I've assumed that you want the end date to be after or equal to the begin date, so I've updated the rule for end accordingly.
Now, when a field is updated and loses focus (on blur), only that field will be validated, and you will get an error message specific to that field if it fails validation.
Remember to handle the display of error messages appropriately in your Livewire component's view:
<div>
<input type="text" wire:model.lazy="name" />
@error('name') <span class="error">{{ $message }}</span> @enderror
<input type="text" wire:model.lazy="begin" />
@error('begin') <span class="error">{{ $message }}</span> @enderror
<input type="text" wire:model.lazy="end" />
@error('end') <span class="error">{{ $message }}</span> @enderror
<!-- Other form fields and buttons -->
</div>
This should give you the desired behavior of validating fields individually as they are updated.
