How to customize $fail message (with html too) to an existing rule ?

For example, you are using ->unique(User::class, 'email'). The default error message will be: "The email has already been taken" since this is coming from the en/validation.php . How about if you need html for that like "This email already exists. <a href="url('forgot-password;)">Forgot Password?</a>" ?
10 Replies
Iliyas M
Iliyas M2y ago
In which page you are using this rule?
vahnmarty
vahnmartyOP2y ago
custom form
TextInput::make('email')->email()->unique(User::class, 'email')
TextInput::make('email')->email()->unique(User::class, 'email')
Iliyas M
Iliyas M2y ago
try to override validate function like this
public function validate(): array
{
if (! count($this->getComponents())) {
return [];
}

$rules = $this->getValidationRules();

if (! count($rules)) {
return [];
}

return $this->getLivewire()->validate($rules, ['email.unique' => 'This email already exists. <a href="url(\'forgot-password\')">Forgot Password?</a>'], $this->getValidationAttributes());
}
public function validate(): array
{
if (! count($this->getComponents())) {
return [];
}

$rules = $this->getValidationRules();

if (! count($rules)) {
return [];
}

return $this->getLivewire()->validate($rules, ['email.unique' => 'This email already exists. <a href="url(\'forgot-password\')">Forgot Password?</a>'], $this->getValidationAttributes());
}
vahnmarty
vahnmartyOP2y ago
Declaration of App\Http\Livewire\Auth\RegisterPage::validate(): array must be compatible with Livewire\Component::validate($rules = null, $messages = [], $attributes = [])
Declaration of App\Http\Livewire\Auth\RegisterPage::validate(): array must be compatible with Livewire\Component::validate($rules = null, $messages = [], $attributes = [])
Up!
Patrick Boivin
@vahnmarty Is this a custom form? An idea would be to add the link conditionally, in a custom View below your form field:
Forms\Components\View::make('forgot-password')
Forms\Components\View::make('forgot-password')
@error('data.email')
<a href=" ... ">Forgot Password?</a>
@enderror
@error('data.email')
<a href=" ... ">Forgot Password?</a>
@enderror
vahnmarty
vahnmartyOP2y ago
I want it below in the email textbox. One thing that worked for me is by using Placeholder
Placeholder::make('existing_email')
->label('')
->dehydrated(false)
->visible(fn(Closure $get) => User::where('email', $get('email'))->exists())
->reactive()
->content(fn() => new HtmlString('<p class="-my-2 text-sm">This email already exists. <a href="forgot-password" class="text-link">Forgot Password?</a></p>')),
Placeholder::make('existing_email')
->label('')
->dehydrated(false)
->visible(fn(Closure $get) => User::where('email', $get('email'))->exists())
->reactive()
->content(fn() => new HtmlString('<p class="-my-2 text-sm">This email already exists. <a href="forgot-password" class="text-link">Forgot Password?</a></p>')),
But this one is using manual validation ->visible(fn(Closure $get) => User::where('email', $get('email'))->exists()) and not the ->unique() . ---- Ok I'll try this View
vahnmarty
vahnmartyOP2y ago
Filament
Fields - Form Builder - Filament
The elegant TALL stack form builder for Laravel artisans.
vahnmarty
vahnmartyOP2y ago
Ah nvm, the i think you are referring to the layout View https://filamentphp.com/docs/2.x/forms/layout#view
Filament
Layout - Form Builder - Filament
The elegant TALL stack form builder for Laravel artisans.
vahnmarty
vahnmartyOP2y ago
There is some awkward space in between.
Patrick Boivin
Ah yes, that's because the View creates an empty row in the layout... You could try a negative margin-top. Maybe your Placeholder was a better idea after all ☝️
Want results from more Discord servers?
Add your server