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
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
I wanted to make a component of these field to use them in more than one form, 10 actually
something like this
I would need to do that on every action I use right?
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)),
]);
yes, but its much cleaner
thanks Dan