F
Filamentβ€’11mo ago
BlackShadow

Custom validation with HTML

Is it possible to use links in validation?
13 Replies
BlackShadow
BlackShadowβ€’11mo ago
$fail(__('The cumulative percentage is higher than 100% <a href="#">Some link here</a>');
$fail(__('The cumulative percentage is higher than 100% <a href="#">Some link here</a>');
Patrick Boivin
Patrick Boivinβ€’11mo ago
Not sure but something you can try:
$fail(new HtmlString(__('...')));
$fail(new HtmlString(__('...')));
BlackShadow
BlackShadowβ€’11mo ago
I tried this, it will just output the HTML 😦
Patrick Boivin
Patrick Boivinβ€’11mo ago
Ok... this would have been too easy πŸ˜„ So I'm not sure how you would go about it correctly, but I think a workaround could be to just output a custom error message in this case, maybe something like this in your Blade template:
@error('my_field')
The cumulative percentage is higher than 100% <a href="#">Some link here</a>
@enderror
@error('my_field')
The cumulative percentage is higher than 100% <a href="#">Some link here</a>
@enderror
BlackShadow
BlackShadowβ€’11mo ago
I'm using the Filament Admin Panel where i have a custom validation for a Select. @pboivin
Patrick Boivin
Patrick Boivinβ€’11mo ago
Ok, makes sense. So maybe one option would be to show a custom error message in a View component... are you familiar with that?
BlackShadow
BlackShadowβ€’11mo ago
I have done it before in Laravel itself, but not sure how i would do it in Filament Panel πŸ€” Example what im currently doing:
Select::make('example')->rules(new CustomRuleExample), // php artisan make:rule CustomRuleExample
Select::make('example')->rules(new CustomRuleExample), // php artisan make:rule CustomRuleExample
https://laravel.com/docs/10.x/validation#using-rule-objects
Patrick Boivin
Patrick Boivinβ€’11mo ago
Yes, I think your approach is correct but the only issue is the HTML doesn't work in the validation error message...
BlackShadow
BlackShadowβ€’11mo ago
So you said you would solve it via a view?
Patrick Boivin
Patrick Boivinβ€’11mo ago
It's a workaround. Basically I'm trying to think of a way for you to display your custom HTML.
BlackShadow
BlackShadowβ€’11mo ago
I mean.. its not a big deal basically sometimes i give a validation error where i indicate what Customer is using something and then that customer name would be a link to the CustomerResource. I was just wondering if it could be done easily, but seems like its not how its meant to work 🀣
Patrick Boivin
Patrick Boivinβ€’11mo ago
Hehe. I feel like there's a way, just I can't quite remember. I'm sure I did it in the past πŸ˜΅β€πŸ’«
BlackShadow
BlackShadowβ€’11mo ago
Hmmm