Pixelairport's avatar

Inject validation rules in filament form field

Does anybody know, if it is possible to do something like:

TextInput::make('title')->rules(['required','max:32'])

instead of

TextInput::make('title')->maxLength(32)->required(),

because I want to use Spatie DTO also for Filament forms or is that a bad idea?

0 likes
4 replies
LaryAI's avatar
Level 58

Certainly! In Filament, form fields like TextInput have their own built-in methods for setting validation rules (e.g., required(), maxLength()). However, if you want to inject raw Laravel validation rules (which can be handy for reusing DTO validation logic), you can use the rules() method directly:

TextInput::make('title')->rules(['required', 'max:32'])

This will work as intended and Laravel will apply these validation rules server-side when the form is submitted. However, do note:

  • When you use rules(), Filament won't automatically handle things like max length on the frontend. For example, the input field itself will not have a maxlength="32" attribute unless you also use maxLength(32).
  • Using DTO (Data Transfer Object) validation from Spatie is fine if you want to keep validation logic centralized, but you may lose some of the automatic client-side helpers that Filament provides.

TL;DR:
You can use rules() like:

TextInput::make('title')->rules(['required', 'max:32'])

But for best UX (client-side validation, attributes, etc.), combine it with Filament’s methods:

TextInput::make('title')
    ->maxLength(32)      // applies frontend attribute
    ->required()         // shows required asterisk
    ->rules(['required', 'max:32']); // shares rules with DTO or request validation

This approach gives you the best of both worlds: DTO/server-side validation with Spatie AND Filament’s frontend polish.

LaryAI's avatar
Level 58

Certainly! In Filament, form fields like TextInput have their own specific methods for validation such as required(), maxLength(), etc., and do not natively support a generic rules() method like Laravel's traditional form requests or validation. So, there's no built-in rules() method in Filament field components.

If you want to keep your validation rules centralized (for example, via a DTO or Form Request), you can leverage Filament's getFormSchema() and use ->rules() on the field (as of Filament v3). For earlier versions, you'll have to map rules to the Filament methods.

Filament v3+ Solution

As of Filament v3, rules() does exist:

TextInput::make('title')->rules(['required', 'max:32'])

However, note:

  • The rules() method attaches Laravel validation rules, but visual cues (like max length counters or required asterisks) will not automatically appear unless you also use the dedicated Filament methods.
  • If you only use rules(), the validation works after submission, not in real-time as you type.

Example Code (Filament v3+):

TextInput::make('title')
    ->rules(['required', 'max:32']);

Best Practice

It's recommended (for best user experience) to combine visual/form input constraints with backend validation:

TextInput::make('title')
    ->required()
    ->maxLength(32);

But you can, if you wish, use only:

TextInput::make('title')
    ->rules(['required', 'max:32']);

And if you're using a DTO or FormRequest for centralizing your rules, you can retrieve them and pass to rules().

Summary

  • Yes, in Filament v3+, you can use ->rules([...]) like you asked.
  • For best experience, combine with Filament's built-in methods.
  • For centralization, inject rules from your DTO or FormRequest into rules().

References:

If you are using an older version (v2 or before), this approach isn't available and you must use the dedicated field methods.

Pixelairport's avatar

Thx @glukinho . I already tried it. Sorry, my question was not so clear. I saw in docs there is rules, but it does not work. But now its clear. It works, but only backend. I hoped it also works for the frontend fields. I will do it the filament way and use my DTOs for the backend and for filament I will go on set everything manual for each field.

Please or to participate in this conversation.