F
Filament16mo ago
sijicc

validation messages under fields not showing

Hi, I have CreateCompany Form component, and validation&model creation is in separate Action CreateCompany. Data is being held in livewire Component $company variable using ->statePath('company'). Problem is - validation is happening for name, website, but filament expects validation messages for company.name company.website. Can I get around this in an easy way, or do I have to do some sheananigans? Related code:
$validated = Validator::make($company, [
'name' => ['required', 'max:255'],
'nip' => ['required', new NipRule(), 'unique:companies,nip'],
'regon' => ['required', new RegonRule(), 'unique:companies,regon'],
'krs' => ['max:10', 'nullable'],
'website' => ['url', 'nullable'],
])->validate()
return Company::create($validated)
$validated = Validator::make($company, [
'name' => ['required', 'max:255'],
'nip' => ['required', new NipRule(), 'unique:companies,nip'],
'regon' => ['required', new RegonRule(), 'unique:companies,regon'],
'krs' => ['max:10', 'nullable'],
'website' => ['url', 'nullable'],
])->validate()
return Company::create($validated)

public function form(Form $form): Form
{
return $form
->schema([
Fieldset::make(__('Basic info'))->schema([
TextInput::make('name')
->required(),
TextInput::make('nip')
->required(),
TextInput::make('regon')
->required(),
TextInput::make('krs'),
TextInput::make('email'),
TextInput::make('phone'),
])
])
->statePath('company');
}
public function form(Form $form): Form
{
return $form
->schema([
Fieldset::make(__('Basic info'))->schema([
TextInput::make('name')
->required(),
TextInput::make('nip')
->required(),
TextInput::make('regon')
->required(),
TextInput::make('krs'),
TextInput::make('email'),
TextInput::make('phone'),
])
])
->statePath('company');
}
public function submit(\App\Actions\CreateCompany $createCompany): RedirectResponse|Redirector
{
$createCompany->handle($this->company);

return redirect()->route('companies.index');
}
public function submit(\App\Actions\CreateCompany $createCompany): RedirectResponse|Redirector
{
$createCompany->handle($this->company);

return redirect()->route('companies.index');
}
And here is validation messages working for company., but not showing without company. Also probably should mention that if i add company. to Validator, it doesn't work 🙂
7 Replies
Iliyas M
Iliyas M16mo ago
Why u validating separately? Just add related rules to each form fields. Filament will handle everything for u.
sijicc
sijiccOP16mo ago
I know about this, but I don't want to duplicate creation logic in 2 places, if that wasn't the case I'd probably use it already.
toeknee
toeknee16mo ago
Just method or function the logic to be re-used.
sijicc
sijiccOP16mo ago
It is reusable(at least it was until I remade the component to filament :D). I just don't have any simple idea to get validation messages to work correctly. I could possibly change validation error names after it fails, but it will be tedious task to do it in every form. I'm asking, since I might be missing something, and it also does not seem like it should be happening.
Iliyas M
Iliyas M16mo ago
You may try this.
public function handle(array $company, string $path = ''): Company
{
$validated = Validator::make($company, [
$path.'name' => ['required', 'max:255'],
$path.'nip' => ['required', new NipRule(), 'unique:companies,nip'],
$path.'regon' => ['required', new RegonRule(), 'unique:companies,regon'],
$path.'krs' => ['max:10', 'nullable'],
$path.'website' => ['url', 'nullable'],
])->validate();

return Company::create($validated);
}
public function handle(array $company, string $path = ''): Company
{
$validated = Validator::make($company, [
$path.'name' => ['required', 'max:255'],
$path.'nip' => ['required', new NipRule(), 'unique:companies,nip'],
$path.'regon' => ['required', new RegonRule(), 'unique:companies,regon'],
$path.'krs' => ['max:10', 'nullable'],
$path.'website' => ['url', 'nullable'],
])->validate();

return Company::create($validated);
}
public function submit(\App\Actions\CreateCompany $createCompany): RedirectResponse|Redirector
{
$createCompany->handle($this->company, 'company.');

return redirect()->route('companies.index');
}
public function submit(\App\Actions\CreateCompany $createCompany): RedirectResponse|Redirector
{
$createCompany->handle($this->company, 'company.');

return redirect()->route('companies.index');
}
sijicc
sijiccOP16mo ago
I tried something like this before, if you do it like this validation messages are shown, but validation itself doesn't work That is, the property "name" is being validated, but errors are being shown for "company.name" property Maybe there is a way to set a name, for which the errors are shown? Or so that I can validate for company.name, and it will both validate & show errors
Want results from more Discord servers?
Add your server