Here is another way. I decided to show instant feedback on the input pre form validation. So with this you get instant feedback on the input.
Mix & Match
use Livewire\Component;
private function getGeneralInformationSchema(): Tabs\Tab
{
return Tabs\Tab::make('General Information')
->columnSpanFull()
->columns(1)
->schema([
Section::make()
->columnSpanFull()
->columns(1)
->schema([
TextInput::make('name')
->required()
->live()
->maxLength(100)
->afterStateUpdated(function (Component $livewire, TextInput $component) {
$livewire->validateOnly($component->getStatePath()); // validate field
})
->label('Name'),
TextInput::make('slug')
->required()
->unique('workspaces', 'slug', $this->workspace)
->live()
->debounce(500) // debounce to prevent too many requests
->afterStateUpdated(function (Component $livewire, TextInput $component) {
$livewire->validateOnly($component->getStatePath()); // validate field
})
->label('Slug'),
Actions::make([
Action::make('updateWorkspace')
->label('Update')
->action(function () {
try {
$this->form->validate(); // @phpstan-ignore-line
$this->updateWorkspace('general_information');
} catch (ValidationException $e) {
$this->setErrorBag($e->validator->errors());
}
})
->requiresConfirmation()
->modalDescription('Are you sure you want to update this workspace information?'),
])->alignEnd(),
])
])
->statePath('data.general_information');
}
Additionally you can also use the disabled method to disable the action if the form is invalid.
That way you have instant feedback on the input.
Action::make('updateWorkspace')
->label('Update')
->action(function () {
$this->updateWorkspace('general_information');
})
->requiresConfirmation()
->disabled(function () {
try {
$this->form->validate(); // @phpstan-ignore-line
return false;
} catch (ValidationException $e) {
$this->setErrorBag($e->validator->errors());
return true;
}
})
->modalDescription('Are you sure you want to update this workspace information?'),