How to format non-native date picker state

What I am trying to do: I'm trying to set the value to a Y-m-d format for a non-native DatePicker What I did: I tried to use different state methods to format the date. My issue/the error: The state is not set correctly, it returns a date that I do not want which results to an exception in other parts of the code. Code:
DatePicker::make('startDateParam')
->label(__('Start date'))
->native(false)
->weekStartsOnMonday()
->displayFormat('d-m-Y')
->closeOnDateSelection()
->beforeStateUpdated(fn (null|string $state) => filled($state) ? Carbon::parse($state)->startOfWeek()->toDateString() : null)
->afterStateUpdated(function ($state) {
if (filled($state)) {
$this->startDate = Carbon::parse($state)->startOfWeek();
$this->startDateParam = $this->startDate->toDateString();
$this->week = $this->startDate->isoWeek;
}
})
->extraAttributes(['class' => 'z-10']),
DatePicker::make('startDateParam')
->label(__('Start date'))
->native(false)
->weekStartsOnMonday()
->displayFormat('d-m-Y')
->closeOnDateSelection()
->beforeStateUpdated(fn (null|string $state) => filled($state) ? Carbon::parse($state)->startOfWeek()->toDateString() : null)
->afterStateUpdated(function ($state) {
if (filled($state)) {
$this->startDate = Carbon::parse($state)->startOfWeek();
$this->startDateParam = $this->startDate->toDateString();
$this->week = $this->startDate->isoWeek;
}
})
->extraAttributes(['class' => 'z-10']),
2 Replies
toeknee
toeknee4w ago
Use an attribute on the model? so startDataParam is always returned as expected?
Merdin
MerdinOP3w ago
startDateParam is an #[Url] attribute But I solved it

Did you find this page helpful?