F
Filament5mo ago
Vp

Access form data inside beforeFormValidated()

I have country code field, I want to save +012 (notice plus sign) in database. but on input, I don't want user to insert "+" so I use mutateFormDataUsing() to append before save, but however this doesn't work with ->unique() validation, it always try to save to DB. My codes:
Forms\Components\TextInput::make('country_code')
->prefix('+')
->unique(ignoreRecord: true),

Actions\CreateAction::make()
->beforeFormValidated(function () {
// how to access form data here
})
Forms\Components\TextInput::make('country_code')
->prefix('+')
->unique(ignoreRecord: true),

Actions\CreateAction::make()
->beforeFormValidated(function () {
// how to access form data here
})
EDIT I notice that mutateFormDataUsing() run after validation, so how can I access form data inside ->beforeFormValidated()?
1 Reply
Tim van Heugten
Tim van Heugten5mo ago
You probably left out some code, but why would you want to do this before validation? Add the validation rules you need (integer, unique, min length 1, max length 3, etc) and then mutate the data after validation (https://filamentphp.com/docs/3.x/panels/resources/creating-records#customizing-data-before-saving).