yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

BaseHtml fix renderSelectOptions for Enum

Open ionutwho opened this issue 2 years ago • 5 comments

Q A
Is bugfix? ✔️
New feature?
Breaks BC?
Fixed issues BaseHtml fix renderSelectOptions for Enum

ionutwho avatar Aug 16 '23 16:08 ionutwho

PR Summary

  • Update to selection conditions in 'BaseHtml.php' The logic for deciding what gets 'selected' in the 'renderSelectOptions' function has been improved. Now, it also checks if the 'selection' is an object, and if that's the case, it makes a comparison based on the 'value' attribute instead. This results in a more versatile and accurate selection process.

what-the-diff[bot] avatar Aug 16 '23 16:08 what-the-diff[bot]

Codecov Report

Patch coverage: 100.00% and project coverage change: -1.29% :warning:

Comparison is base (73902f0) 48.90% compared to head (bb7af07) 47.61%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #19928      +/-   ##
==========================================
- Coverage   48.90%   47.61%   -1.29%     
==========================================
  Files         445      445              
  Lines       42806    42806              
==========================================
- Hits        20935    20383     -552     
- Misses      21871    22423     +552     
Files Changed Coverage Δ
framework/helpers/BaseHtml.php 99.33% <100.00%> (ø)

... and 15 files with indirect coverage changes

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Aug 16 '23 16:08 codecov[bot]

I also think that using instanceof UnitEnum would be better - it should be safer than changing behavior for all objects.

And IMO it should be done at the beginning of $selection processing:

if ($selection !== null) {
    if ($selection instanceof \UnitEnum) {
        $selection = $selection->value;
    }
    ...

rob006 avatar Aug 17 '23 09:08 rob006

I also think that using instanceof UnitEnum would be better - it should be safer than changing behavior for all objects.

And IMO it should be done at the beginning of $selection processing:

if ($selection !== null) {
    if ($selection instanceof \UnitEnum) {
        $selection = $selection->value;
    }
    ...

Well i tried your fix but its not working because we are using this: https://github.com/spatie/enum Its not really an Enum, actually its a Class extending an Enum class which doesnt implement UnitEnum... abstract class Enum extends \Spatie\Enum\Enum abstract class Enum implements JsonSerializable

ionutwho avatar Aug 17 '23 20:08 ionutwho

I don't think we should support any object here, this should be out of the scope of the framework and it should depend on the developers to extend framework functionality in their own app to support any custom solution like in your case. Still, we would accept real enums here.

bizley avatar Aug 18 '23 11:08 bizley

are you submitting fixes for some of your internal code to the public? Why are you not checking the type of value property then? Another useless PR. @samdark I hope we're not thinking of adapting this

Webkadabra avatar Feb 26 '24 17:02 Webkadabra

Is this PR ready?

terabytesoftw avatar Mar 21 '24 17:03 terabytesoftw

Hey, this issue can be closed, we took a different approach so Is no longer needed. It was a bug on the framework regarding support for enums. If I remember correctly if we used an enum object the render option wasn't working.

cristiangirlea avatar Mar 21 '24 17:03 cristiangirlea

I'm closing it then. Still, PR introducing support for enums is welcome.

bizley avatar Mar 22 '24 07:03 bizley