Save hidden field

Is there any better way to apply the following
Forms\Components\Toggle::make('expires')->reactive()->afterStateUpdated(
function ($state, callable $set) {
if (!$state) { $set('expire_date', null);
$set('expireDate', null);
}})
->inline(false)->required(),
Hidden::make('expire_date')->afterStateHydrated(
function ($state,$set){
$set('expireDate', $state);
}),
Forms\Components\DatePicker::make('expireDate')->closeOnDateSelection()->visible(fn(callable $get) => $get('expires'))->afterStateUpdated(
function ($state,$set){
$set('expire_date', $state);
}),
Forms\Components\Toggle::make('expires')->reactive()->afterStateUpdated(
function ($state, callable $set) {
if (!$state) { $set('expire_date', null);
$set('expireDate', null);
}})
->inline(false)->required(),
Hidden::make('expire_date')->afterStateHydrated(
function ($state,$set){
$set('expireDate', $state);
}),
Forms\Components\DatePicker::make('expireDate')->closeOnDateSelection()->visible(fn(callable $get) => $get('expires'))->afterStateUpdated(
function ($state,$set){
$set('expire_date', $state);
}),
4 Replies
Dan Harrin
Dan Harrin2y ago
i wouldnt keep it simple and do this work when the form is actually submitted so in mutateFormDataBeforeSave() etc, if the record is not expiring then set the field to null there
Majid Al Zariey
Majid Al ZarieyOP2y ago
I wanted to make a component of these field to use them in more than one form, 10 actually something like this
return Grid::make(["sm" => 3])->schema([
DatePicker::make($startDate)
->closeOnDateSelection()
->displayFormat('M-Y')
->weekStartsOnSunday()
->formatStateUsing(fn($state) => $state == null ? null : Carbon::parse($state)->format('Y-m-01'))
->label($startLabel)
->nullable(),
DatePicker::make($endDate.'picker')
->visible(fn(callable $get) => ($reverse && $get($toggle)) || (!$reverse && !$get($toggle)) )
->closeOnDateSelection()
->weekStartsOnSunday()
->displayFormat('M-Y')
->formatStateUsing(fn($state) => $state == null ? '' : Carbon::parse($state)->format('Y-m-01'))
->requiredWith(fn()=>!$reverse? $startDate:$toggle)
->nullable()
->afterStateUpdated(fn ($state,$set)=>$set($endDate, $state))
->after($startDate)
->label($endLabel),
Toggle::make($toggle)->reactive()->label($toggleLabel)
->afterStateUpdated(function ($state, callable $set) use ($reverse, $endDate) {
if (($reverse && !$state) || (!$reverse && $state)) {
$set($endDate, null);
$set($endDate.'picker', null);
}
})->inline(false)->required()
->formatStateUsing( fn($get)=>($get($startDate) != null && $get($endDate) == null && !$reverse) || ( $get($endDate) != null && $reverse)),
Hidden::make($endDate)
->afterStateHydrated(fn($state, $set)=>$set($endDate.'picker', $state)),
]);
return Grid::make(["sm" => 3])->schema([
DatePicker::make($startDate)
->closeOnDateSelection()
->displayFormat('M-Y')
->weekStartsOnSunday()
->formatStateUsing(fn($state) => $state == null ? null : Carbon::parse($state)->format('Y-m-01'))
->label($startLabel)
->nullable(),
DatePicker::make($endDate.'picker')
->visible(fn(callable $get) => ($reverse && $get($toggle)) || (!$reverse && !$get($toggle)) )
->closeOnDateSelection()
->weekStartsOnSunday()
->displayFormat('M-Y')
->formatStateUsing(fn($state) => $state == null ? '' : Carbon::parse($state)->format('Y-m-01'))
->requiredWith(fn()=>!$reverse? $startDate:$toggle)
->nullable()
->afterStateUpdated(fn ($state,$set)=>$set($endDate, $state))
->after($startDate)
->label($endLabel),
Toggle::make($toggle)->reactive()->label($toggleLabel)
->afterStateUpdated(function ($state, callable $set) use ($reverse, $endDate) {
if (($reverse && !$state) || (!$reverse && $state)) {
$set($endDate, null);
$set($endDate.'picker', null);
}
})->inline(false)->required()
->formatStateUsing( fn($get)=>($get($startDate) != null && $get($endDate) == null && !$reverse) || ( $get($endDate) != null && $reverse)),
Hidden::make($endDate)
->afterStateHydrated(fn($state, $set)=>$set($endDate.'picker', $state)),
]);
I would need to do that on every action I use right?
Dan Harrin
Dan Harrin2y ago
yes, but its much cleaner
Majid Al Zariey
Majid Al ZarieyOP2y ago
thanks Dan
Want results from more Discord servers?
Add your server