z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.14k stars 2.81k forks source link

In update form: Radio buttons and select inputs are not being selected in case of enums (string keys of options) #5833

Closed amismailz closed 9 months ago

amismailz commented 11 months ago

Description:

While using dropdowns / radio buttons in the update form, saved values are not being selected if options have string keys but works with integer ones.

Steps To Reproduce:

Not working (No inputs are selected)

// $form->model()->gender = 'male'
$form->radio('gender', __('Gender'))->options(['male' => 'male', 'female' => 'female']);`
$form->select('gender', __('Gender'))->options(['male' => 'male', 'female' => 'female']);`

Works (Male is selected)

// $form->model()->gender = 1
$form->radio('gender', __('Gender'))->options([1 => 'male', 2 => 'female']);`
$form->select('gender', __('Gender'))->options([1 => 'male', 2 => 'female']);`
fits-fujiura-st commented 11 months ago

@amismailz

Hi, following code works in my environment.

    protected function form()
    {
        $form = new Form(new User());

        $form->text('name', __('Name'));
        $form->email('email', __('Email'));

        $form->radio('sex1', 'Sex(number)')->options([0=>'male', 1=>'female', 2=>'other'])->default(0);
        $form->radio('sex2', 'Sex(string)')->options(['male'=>'male', 'female'=>'female', 'other'=>'other'])->default('male');

        $form->datetime('email_verified_at', __('Email verified at'))->default(date('Y-m-d H:i:s'));
        $form->password('password', __('Password'));
        $form->text('remember_token', __('Remember token'));

        return $form;
    }

Snapshot of DB

image

Snapshot of editing user id=1 image

Snapshot of editing user id=3 image

Is it possible to check the actual DB values?

amismailz commented 11 months ago

@fits-fujiura-st What is the field data type you use? did you use enum?

fits-fujiura-st commented 11 months ago

@amismailz

I used int for number and varchar for string.

image
amismailz commented 11 months ago

@fits-fujiura-st I just got the cause of this issue, this was because I make some getter functions (attribute accessors) like this:

public function getGenderAttribute($value)
    {
        return __(ucfirst($value));
    }

I'll try another way to handle this.

Thank you so much, I really appreciate your great support :+1: