afterStateUpdated() function does not work when hidden() is true
Toggle::make('kismi_teklife_acik_mi')
->live()
->requiredWith('kisim_sayisi')
->inline(false)
->afterStateUpdated(function (Get $get, Set $set) {
if (($get('kismi_teklife_acik_mi') == false)) {
$set('kisim_sayisi', null);
}
})
->label('İhale Kısmi Teklife Açık Mı?'),
TextInput::make('kisim_sayisi')
->label('Kısım Sayısı')
->requiredWith('kismi_teklife_acik_mi')
->numeric()
->validationAttribute('Kısım Sayısı')
->hidden(
fn (Get $get): bool => !$get('kismi_teklife_acik_mi')
),
Hello, I want to empty the kisim_sayisi field when the kismi_teklife_acik_mi field is closed. If it is visible, it works, but if it is not visible, it does not assign null. In short, afterStateUpdated() does not work when hidden() is true.
4 Replies
This is correct! Hidden fields are removed from the request as they are no longer part of it. What you should do, is mutate the data before saving or creating to null it if that field is true. This prevents people manipluating the data in the browser.
How can I empty the area before it is hidden?
Just mutate it on the save?
Yes, this might work for me too.