Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Pixelairport's avatar

Filament validation not use Resource file

I create test for my website and found out that filament, does not use my resource file. I have this im my resource file:

return [
  'name' => ['required'],
  'email' => ['required', 'email', 'max:254']
];

But when i create an item with email 'abc' it works. The string is saved. Only name must be required, because i set this up in DB. Is that normal? Does it make sense that filament dont use the validation? Or can I setup that it will use this validation. Because I also have a second panel vor my users and I want to use a good validation.

0 likes
4 replies
LaryAI's avatar
Level 58

In Filament, validation rules are typically defined within the form component of your resource, rather than in a separate resource file. If you want to ensure that your validation rules are applied when creating or updating a resource, you need to define them in the form() method of your Filament resource class.

Here's how you can set up validation rules in a Filament resource:

  1. Open your Filament resource class, which is usually located in the App\Filament\Resources directory.

  2. Locate the form() method. If it doesn't exist, you can create it.

  3. Define your validation rules within the form() method using the ->rules() method on the form fields.

Here's an example of how you can set up the validation rules for the name and email fields:

use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use App\Models\YourModel;

class YourResource extends Resource
{
    protected static string $model = YourModel::class;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->required()
                    ->rules(['required']),

                Forms\Components\TextInput::make('email')
                    ->required()
                    ->rules(['required', 'email', 'max:254']),
            ]);
    }

    // Other methods...
}

In this example, the TextInput components for name and email have validation rules applied directly within the form() method. The ->rules() method is used to specify the validation rules for each field.

Make sure to replace YourModel and YourResource with the actual names of your model and resource classes.

By setting up your validation rules in this way, Filament will automatically apply them when creating or updating records through the resource's form.

GHOST117s's avatar

U can write Ur own custom rule and use it

 TextInput::make('expDate')
                        ->label('Expiry Date')
                        ->required()
                        ->mask('99/99')
                        ->regex('/^(0[1-9]|1[0-2])\/?([0-9]{2})$/')
                        ->rules([new CardExpiryDate()])
                        ->maxLength(5),
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class CardExpiryDate implements ValidationRule
{

    public function validate(string $attribute, mixed $value, Closure $fail): void
    {

        list($month, $year) = sscanf($value, '%2d/%2d');
        $currentDate = now();
        $expiryDate = now()->setMonth($month)->setYear(2000 + $year);
        $maxExpiryDate = $currentDate->copy()->addYears(15);
        if ($year == $currentDate->format('y') && $month == $currentDate->format('m')) {
            $fail("The expiry date is invalid.");
            return;
        }
        if ($expiryDate->greaterThanOrEqualTo($currentDate) && $expiryDate->lessThanOrEqualTo($maxExpiryDate)) {
            return;
        }

        $fail("The expiry date is invalid.");
    }
}

and like laravel u can put model level validation like

public static $rules = [
        'contact_id' => 'nullable|string|exists:contacts,uq_contact_id',
]
Pixelairport's avatar

Ok. Thx. I thought I can use the Request file like I inject it in a controller to validate. So I have the same logic in two different files? And need to test each of them? Normally I have a request file like this:

class AuthorRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'name' => ['required'],
            'email' => ['required', 'email', 'max:254'],
        ];
    }

    public function authorize(): bool
    {
        return true;
    }
}

There is no way to use this in filament form?

Pixelairport's avatar
Pixelairport
OP
Best Answer
Level 12

Ok. I think I have a solution. I did not know, that the rules are backend server rules. I thought i can only define frontend rules for html. I did not found a method rules, for the whole form. Is there one? At the moment, this is my solution:

// Get rules from my request file
TextInput::make('name')->rules((new AuthorRequest)->rules()['name']),

Please or to participate in this conversation.