Add PHP enum support in BaseHtml and RangeValidator#20794
Open
WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
Open
Add PHP enum support in BaseHtml and RangeValidator#20794WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
BaseHtml and RangeValidator#20794WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #20794 +/- ##
============================================
+ Coverage 80.13% 80.20% +0.06%
- Complexity 11533 11552 +19
============================================
Files 374 374
Lines 30206 30234 +28
============================================
+ Hits 24207 24249 +42
+ Misses 5999 5985 -14 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Fixes Yii2 handling of PHP 8.1+ enums in HTML helpers and range validation, and adds enum-driven configuration for RangeValidator.
Changes:
- Convert enum attribute values to scalar (
->value) / name (->name) inBaseHtml::getAttributeValue(). - Add
enum+targetconfiguration toRangeValidatorand makegetClientOptions()robust to enum cases inrange. - Add enum stubs and PHPUnit coverage for the new enum behavior.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/framework/validators/stubs/EnumStubs.php | Adds backed + unit enum fixtures used by tests. |
| tests/framework/validators/RangeValidatorEnumTest.php | Covers enum-based RangeValidator config and client options handling. |
| tests/framework/helpers/HtmlEnumTest.php | Covers enum conversion behavior in Html::getAttributeValue(). |
| phpstan-baseline-7x.neon | Baselines PHPStan errors for enum symbols on PHP 7.4 analysis. |
| framework/validators/RangeValidator.php | Adds enum/target properties and enum-aware client options generation. |
| framework/helpers/BaseHtml.php | Converts enum attribute values to scalar/name (including arrays). |
| framework/CHANGELOG.md | Documents the bugfix + enhancement entries. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes PHP enum support in
BaseHtml::getAttributeValue()andRangeValidator::getClientOptions(), and addsenum/targetproperties toRangeValidatorfor convenient enum-based validation.Problem
When a model attribute value is a PHP 8.1+ enum,
BaseHtml::getAttributeValue()returns the enum object as-is, causing "Object could not be converted to string" errors in HTML helpers. Similarly,RangeValidator::getClientOptions()crashes on(string) $valuewhen the range contains enum cases.Changes
Bugfix:
BaseHtml::getAttributeValue()now convertsBackedEnumto->valueandUnitEnumto->name(single values and arrays).Bugfix:
RangeValidator::getClientOptions()handles enum cases in range without crashing.Feature: New
enumandtargetproperties onRangeValidator:The approach follows the existing pattern from
yii\db\Commandandyii\db\ColumnSchema.Alternative implementation to #20318.
Coverage