glhd / aire

Modern form builder for Laravel
https://airephp.com
MIT License
542 stars 36 forks source link

Data Binding does not work with Enum-casted properties #110

Closed Bbansjkl closed 2 years ago

Bbansjkl commented 2 years ago

Describe the bug If a model instance has a property that is casted to an Enum, data binding for this property doesn't work: When loading a form, the element will be empty.

This is likely because the property of the instance is an enum instance, not a string. The correct value can be gotten by accessing $model->enum_property->value.

Have you published the Aire config file? Yes

Have you added custom Aire views? No

What version does this affect?

To Reproduce Steps to reproduce the behavior:

  1. Create a Model with an Enum property (see below)
  2. Create View with a bound input for said property
  3. Create instance of Model
  4. Open an edit view of instance and check the input element for the property

Expected behavior The input field should be set to the value-property of the Enum-casted property

Additional context

Example: Model with Enum property ```php EnumProperty::class, ]; } ```
Example: Migration for Model ```php id(); $table->string('enum_property'); $table->timestamps(); }); } ```
Example: Enum-class ```php
Example: View ```php route('example.update',$example->id) ->bind($example) ->put() ->enctype('multipart/form-data') }} @php $propertySelectList = [ '' => 'Nothing', ]; foreach (EnumProperty::values() as $value) { $propertySelectList[$value] = $value; } @endphp {{ Aire::select($propertySelectList,'enum_property', 'Select Property')}} {{ Aire::submit('Save') }} ```

A workaround exists:

{{ Aire::select($propertySelectList,'enum_property', 'Select Property')
     ->value(old('enum_property') ?? $example->enum_property->value ?? '') }}