Weird things occur when setting and unsetting a DateTimePicker value

When trying to set (or unset) the value of a DateTimePicker, weird things happen. I prepared this example to show what I'm experiencing. Please, let me know if you experience the same, or if I'm missing something.
Select::make('status')
->required()
->live()
->options([
'draft' => trans('Draft'),
'published' => trans('Published'),
])
->afterStateUpdated(function ($state, Set $set) {
$published = ($state === 'published') ? now() : null;

$set('text_published_at', $published);
$set('non_native_date_published_at', $published);
$set('native_date_published_at', $published);
}),

TextInput::make('text_published_at'),

DateTimePicker::make('non_native_date_published_at')->native(false),

DateTimePicker::make('native_date_published_at')->native(true),
Select::make('status')
->required()
->live()
->options([
'draft' => trans('Draft'),
'published' => trans('Published'),
])
->afterStateUpdated(function ($state, Set $set) {
$published = ($state === 'published') ? now() : null;

$set('text_published_at', $published);
$set('non_native_date_published_at', $published);
$set('native_date_published_at', $published);
}),

TextInput::make('text_published_at'),

DateTimePicker::make('non_native_date_published_at')->native(false),

DateTimePicker::make('native_date_published_at')->native(true),
Add the fields above to a form and then change the value of the first select. - When the value is set to Draft, the fields below are expected to be emptied as it occurs with the text input. - When the value is set to Published, the fields below are expected to be updated with the current timestamp as it occurs with the text input. But the date time pickers don't seem to like being updated: - The non native field accepts a new date when it's initially empty but rather than being cleared when the select is set to Draft, it changes to a different date time value (which isn't even the current timestamp). - The native field is worse, as it doesn't even change no matter what I do with the select.
2 Replies
torriv
torriv15mo ago
Just add ->format('Y-m-d H:i:s') to now(). This worked for me:
Select::make('status')
->required()
->live()
->options([
'draft' => trans('Draft'),
'published' => trans('Published'),
])
->afterStateUpdated(function ($state, Set $set) {
$published = ($state === 'published') ? now()->format('Y-m-d H:i:s') : null;

$set('text_published_at', $published);
$set('non_native_date_published_at', $published);
$set('native_date_published_at', $published);
}),

TextInput::make('text_published_at'),

DateTimePicker::make('non_native_date_published_at')->native(false),

DateTimePicker::make('native_date_published_at'),
Select::make('status')
->required()
->live()
->options([
'draft' => trans('Draft'),
'published' => trans('Published'),
])
->afterStateUpdated(function ($state, Set $set) {
$published = ($state === 'published') ? now()->format('Y-m-d H:i:s') : null;

$set('text_published_at', $published);
$set('non_native_date_published_at', $published);
$set('native_date_published_at', $published);
}),

TextInput::make('text_published_at'),

DateTimePicker::make('non_native_date_published_at')->native(false),

DateTimePicker::make('native_date_published_at'),
KeyMe
KeyMe13mo ago
any update?

Did you find this page helpful?