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
Maybe you can mutate the form data before saving. Here is how
https://filamentphp.com/docs/3.x/panels/resources/creating-records#customizing-data-before-saving
You could probably not use a hidden field in this case
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?
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?
have you tried using the
rules()
method instead of doing the validation check in afterStateUpdated()
?
Also, did you know you can call pluck()
as a query instead of a collection method?
Same results and phpstan
won't complain about it πi was trying with rules - it was not working for me - let me try again
I'd try this as well on the state_id
->live(onBlur: true)
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
Woot Woot ππ»