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:
-
Open your Filament resource class, which is usually located in the
App\Filament\Resourcesdirectory. -
Locate the
form()method. If it doesn't exist, you can create it. -
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.