robsontenorio / mary

Laravel Blade UI Components for Livewire 3
https://mary-ui.com
Other
874 stars 104 forks source link

Datetime on edit #517

Closed flatcapco closed 3 weeks ago

flatcapco commented 1 month ago

maryUI version

latest

daisyUI version

latest

Livewire version

latest

What browsers are affected?

Chrome

What happened?

I'm trying to create an edit page following the flow example, I've been getting on really well but come across a snag using datetime, it doesn't hydrate the data so the field shows empty. It updates just fine if I select a value. The create screen also works as expected.

In the edit blade file:

    #[Rule('required')]
    public string $start_datetime = '';
    ....

            <x-mary-datetime label="Start Date" wire:model="start_datetime"/>

In my model:

    protected function casts(): array
    {
        return [
            'name'           => 'string',
            'start_datetime' => 'datetime',
            'end_datetime'   => 'datetime',
        ];
    }

Is there a specific cast its expecting on this view?

robsontenorio commented 1 month ago

Have you tried “date time:Y-m-d H:i” ?

flatcapco commented 1 month ago

Have you tried “date time:Y-m-d H:i” ?

I was just about to reply - looks like datetime:Y-m-d works as a cast, but I don't really want to modify the main cast if possible - is there another way to format it before passing it into the mary component? or should I just create a separate associated field and do the formatting there?

flatcapco commented 1 month ago

        $this->start_date = $this->season->start_datetime->format('Y-m-d');
        $this->end_date = $this->season->end_datetime->format('Y-m-d');
    }

    public function save(): void
    {
        $data = $this->validate();

        $this->season->update([
            'name'           => $data['name'],
            'start_datetime' => Carbon::parse($data['start_date'])->startOfDay(),
            'end_datetime'   => Carbon::parse($data['end_date'])->endOfDay(),
        ]);

Do you think this is a clean enough way to handle this?

robsontenorio commented 3 weeks ago

It really depends of your use case, and it is out of the scope of this package. Glad it works for you ;)