Validation doesn't work on form created with createOptionForm

Folks, I used the createOptionForm function to enable a new record creation form in a select field. Unfortunately, on this form, the unique() validation is not working. I followed a tip from Povilas in the code below, also without success:
// Document comes from a Leandro Ferreira package for document fields in pt_BR
Document::make('cpf')
->label('CPF')
->live()
->cpf('999.999.999-99')
->dehydrateStateUsing(fn(string $state): string => preg_replace('/[.-]+/', '', $state))
->unique()
->required()
->live()
->afterStateUpdated(function (Forms\Contracts\HasForms $livewire, Document $component) {
$livewire->validateOnly($component->getStatePath());
}),
// Document comes from a Leandro Ferreira package for document fields in pt_BR
Document::make('cpf')
->label('CPF')
->live()
->cpf('999.999.999-99')
->dehydrateStateUsing(fn(string $state): string => preg_replace('/[.-]+/', '', $state))
->unique()
->required()
->live()
->afterStateUpdated(function (Forms\Contracts\HasForms $livewire, Document $component) {
$livewire->validateOnly($component->getStatePath());
}),
It only validates required and not unique. When submitting the form, the key violation error occurs, as I tested an already registered document number. Can anyone tell me where I'm going wrong?
Solution:
I implemented something similar, but without using any third party packages (Document) and the validation works as expected. Here's an example of a dropdown with the option to create a new user. The unique validation is added to the email field ```php Forms\Components\Select::make('owner_id') ->relationship('owner', 'name')...
Jump to solution
4 Replies
tuto1902
tuto190214mo ago
Thank you for your question. Allow me some time to do my due research. I'm sure we can figure it out.
Solution
tuto1902
tuto190214mo ago
I implemented something similar, but without using any third party packages (Document) and the validation works as expected. Here's an example of a dropdown with the option to create a new user. The unique validation is added to the email field
Forms\Components\Select::make('owner_id')
->relationship('owner', 'name')
->native(false)
->createOptionForm([
Forms\Components\TextInput::make('name')
->required(),
Forms\Components\TextInput::make('email')
->email()
->required()
->unique()
->live()
->afterStateUpdated(function (Forms\Contracts\HasForms $livewire, TextInput $component) {
$livewire->validateOnly($component->getStatePath());
})
])
Forms\Components\Select::make('owner_id')
->relationship('owner', 'name')
->native(false)
->createOptionForm([
Forms\Components\TextInput::make('name')
->required(),
Forms\Components\TextInput::make('email')
->email()
->required()
->unique()
->live()
->afterStateUpdated(function (Forms\Contracts\HasForms $livewire, TextInput $component) {
$livewire->validateOnly($component->getStatePath());
})
])
Is it possible this is an issue with the Document field itself?
tuto1902
tuto190214mo ago
No description
tuto1902
tuto190214mo ago
@cpereiraweb Have you had any luck with this?
Want results from more Discord servers?
Add your server