Skip to content

Add PHP enum support in BaseHtml and RangeValidator#20794

Open
WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
WarLikeLaux:extend-enum-support
Open

Add PHP enum support in BaseHtml and RangeValidator#20794
WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
WarLikeLaux:extend-enum-support

Conversation

@WarLikeLaux
Copy link
Contributor

@WarLikeLaux WarLikeLaux commented Mar 12, 2026

Q A
Is bugfix? ✔️
New feature? ✔️
Tests added? ✔️
Breaks BC?
Fixed issues -

What does this PR do?

Fixes PHP enum support in BaseHtml::getAttributeValue() and RangeValidator::getClientOptions(), and adds enum/target properties to RangeValidator for 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) $value when the range contains enum cases.

Changes

Bugfix: BaseHtml::getAttributeValue() now converts BackedEnum to ->value and UnitEnum to ->name (single values and arrays).

Bugfix: RangeValidator::getClientOptions() handles enum cases in range without crashing.

Feature: New enum and target properties on RangeValidator:

['status', 'in', 'enum' => Status::class]                     // validate against backed values (default)
['status', 'in', 'enum' => Status::class, 'target' => 'name'] // validate against case names
['suit', 'in', 'enum' => Suit::class, 'target' => 'name']     // unit enums (name only)

The approach follows the existing pattern from yii\db\Command and yii\db\ColumnSchema.

Alternative implementation to #20318.

Coverage

File Tests Lines
RangeValidator.php 10 → 23 17/38 (45%) → 48/56 (86%)
BaseHtml.php +6 (enum-specific) getAttributeValue enum branches covered

@codecov
Copy link

codecov bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 96.55172% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 80.20%. Comparing base (37b6e7b) to head (8356d80).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
framework/validators/RangeValidator.php 95.23% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) in BaseHtml::getAttributeValue().
  • Add enum + target configuration to RangeValidator and make getClientOptions() robust to enum cases in range.
  • 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.

@WarLikeLaux WarLikeLaux marked this pull request as ready for review March 13, 2026 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants