F
Filamentβ€’15mo ago
blakdronzer

Need help updating value of hidden field via retrieving value from database and set

I have a scenario where user selects a value from dropdown and post that, we need to update the value of a hidden field basis on the record from the database. The very reason for the same is - it is also required for validation of other field. Can anyone help for the same?
9 Replies
tuto1902
tuto1902β€’15mo ago
You could probably not use a hidden field in this case
blakdronzer
blakdronzerOPβ€’15mo ago
Before saving is not a problem the issue is i need to put in validation basis the selection of a value from dropdown Select::make('state_id') ->label('State') ->options(State::all()->pluck('state_name', 'id')) ->searchable() ->afterStateUpdated(function (Get $get, Set $set, Closure $fail, ?string $state) { if(filled($state)) { $obj = State::where('id', $state)->first(); $set('state_code', $obj->gst_code);
if (filled($get('gst_number'))) { $gst_number = $get('gst_number'); $state_code = $get('state_code'); if (trim(substr($gst_number, 0, 2)) !== $state_code) { $fail("GST Number dose not match with the selected State."); } } } }) ->reactive(), here is how i am trying now issue when i am trying using Closure $fail - it gives error Target [Closure] is not instantiable. @tuto1902 - can you possibly help in here?
tuto1902
tuto1902β€’15mo ago
have you tried using the rules() method instead of doing the validation check in afterStateUpdated()?
use Closure;
use Filament\Forms\Get;

TextInput::make('slug')->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
if ($get('other_field') === 'foo' && $value !== 'bar') {
$fail("The {$attribute} is invalid.");
}
},
])
use Closure;
use Filament\Forms\Get;

TextInput::make('slug')->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
if ($get('other_field') === 'foo' && $value !== 'bar') {
$fail("The {$attribute} is invalid.");
}
},
])
Also, did you know you can call pluck() as a query instead of a collection method?
->options(State::pluck('state_name', 'id'))
->options(State::pluck('state_name', 'id'))
Same results and phpstan won't complain about it πŸ˜‰
blakdronzer
blakdronzerOPβ€’15mo ago
i was trying with rules - it was not working for me - let me try again
tuto1902
tuto1902β€’15mo ago
I'd try this as well on the state_id ->live(onBlur: true)
blakdronzer
blakdronzerOPβ€’15mo ago
sorry to say but rule dose not work out for me now 😦 Select::make('state_id') ->label('State') ->options(State::all()->pluck('state_name', 'id')) ->searchable() ->rules([ fn (Get $get, Set $set): Closure => function (string $attribute, $value, Closure $fail) use ($get, $set) { Notification::make() ->title("Validating the rule") ->danger() ->send(); if(filled($value)) { $obj = State::where('id', $value)->first(); $set('state_code', $obj->gst_code); $gst_number = $get('gst_number'); Notification::make() ->title("GST Number to " . $gst_number ) ->danger() ->send(); $state_code = $get('state_code'); if ($gst_number !== '') { if (trim(substr($gst_number, 0, 2)) !== $state_code) { $fail("GST Number dose not match with the selected State."); } } }, ]) ->reactive(), no notification gets popped up with afterStateUpdated - i am able to change get the notification atleast - it is working but this rule is not working at all This is not a problem - i have already done this part .. not an issue .. mainly want the form to fail if the the field value is not set correctly Select::make('state_id') ->label('State') ->options(State::all()->pluck('state_name', 'id')) ->searchable() ->afterStateUpdated(function (Set $set, $livewire, ?string $state) { if(filled($state)) { $obj = State::where('id', $state)->first(); $state_code = $obj->gst_code; if (filled('gst_number')) { $gst_number = $livewire->data['gst_number']; if (trim(substr($gst_number, 0, 2)) !== $state_code) { Notification::make() ->title("GST Number dose not match with the selected State.") ->danger() ->send(); } } } }) ->reactive(), i have managed to reach the state where i am able to validate if the value matches of not - but the issue is forcefully invalidating the field for error can any1 help in for here? Got it - i added the rule to the main field to be validated before submission - so now it fails correctly!! Thank you
tuto1902
tuto1902β€’15mo ago
Woot Woot πŸ™ŒπŸ»
Want results from more Discord servers?
Add your server