Skip to content

Conversation

terabytesoftw
Copy link
Member

@terabytesoftw terabytesoftw commented Sep 27, 2025

Q A
Is bugfix?
New feature? ✔️
Breaks BC?

Previous Behavior:

  • label() generates a standard <label> tag with the model attribute label.
$field->label();
// output: <label class="control-label" for="model-attribute">Attribute Name</label>
  • label('Custom Text') generates a <label> tag with custom text.
$field->label('My Label');
// output: <label class="control-label" for="model-attribute">My Label</label>
  • label(false) removes the label entirely from the field (empty string).
$field->label(false);
// output: '' (empty string, no label rendered)
  • label($text, $options) generates a label with custom options merged with labelOptions.
$field->label('My Label', ['class' => 'custom-class']);
// output: <label class="custom-class" for="model-attribute">My Label</label>

New Behavior:

tag => 'label' default uses Html::activeLabel() to generate a standard <label> element with the for attribute.

$field->label(options: ['label' => 'My Label']);
// output: <label class="control-label" for="model-attribute">My Label</label>

tag => false renders the label content as raw HTML without any wrapper tag.

$field->label(
    options: [
        'label' => 'My Label',
        'tag' => false,
    ],
);
// output: My Label

tag => span/div/h3/etc - uses Html::tag() to generate the specified element.

$field->label(
    'Form Section',
    [
        'class' => 'section-title',
        'tag' => 'h3',
    ],
);
// output: <h3 class="section-title">Form Section</h3>

… with `enclosedByLabel = false` in `checkbox`/`radio` methods (yiisoft#20537).
Copy link

codecov bot commented Sep 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 64.50%. Comparing base (d245fc2) to head (845442c).
⚠️ Report is 1 commits behind head on 22.0.

Additional details and impacted files
@@             Coverage Diff              @@
##               22.0   #20546      +/-   ##
============================================
- Coverage     65.61%   64.50%   -1.11%     
- Complexity    11263    11267       +4     
============================================
  Files           428      428              
  Lines         37068    37083      +15     
============================================
- Hits          24323    23922     -401     
- Misses        12745    13161     +416     

☔ 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.

@xicond
Copy link
Contributor

xicond commented Sep 27, 2025

maybe can reduce redundant code ? so it's more easy to maintain later

@terabytesoftw
Copy link
Member Author

maybe can reduce redundant code ? so it's more easy to maintain later

Yes it is a good idea.

@xicond
Copy link
Contributor

xicond commented Sep 28, 2025

I'm thinking,
if this way can support custom tag
why dont make it consistent for

$field->checkbox( )->label('Custom Label', 'labelOptions' => [
    'class' => 'radio-option-label',
    'data-value' => 'choice-1',
    'tag' => 'span',
]);

@terabytesoftw
Copy link
Member Author

I'm thinking, if this way can support custom tag why dont make it consistent for

$field->checkbox( )->label('Custom Label', 'labelOptions' => [
    'class' => 'radio-option-label',
    'data-value' => 'choice-1',
    'tag' => 'span',
]);

If it's a good idea, i'll do it in another PR.

@terabytesoftw terabytesoftw changed the title feat(activefield): labelOptions are ignored when using custom label with enclosedByLabel = false in checkbox/radio methods (#20537). feat(activefield): enhance label() method with tag option and fix labelOptions for checkbox/radio (#20537). Sep 28, 2025
@terabytesoftw terabytesoftw changed the title feat(activefield): enhance label() method with tag option and fix labelOptions for checkbox/radio (#20537). feat(activefield): Enhance label() method with tag option and fix labelOptions for checkbox/radio (#20537). Sep 28, 2025
@terabytesoftw
Copy link
Member Author

I'm thinking, if this way can support custom tag why dont make it consistent for

$field->checkbox( )->label('Custom Label', 'labelOptions' => [
    'class' => 'radio-option-label',
    'data-value' => 'choice-1',
    'tag' => 'span',
]);

done.

@terabytesoftw terabytesoftw changed the title feat(activefield): Enhance label() method with tag option and fix labelOptions for checkbox/radio (#20537). Enhance label() method in ActiveField with tag option and fix labelOptions for checkbox/radio (#20537). Sep 29, 2025
@samdark samdark requested a review from Copilot September 29, 2025 22:29
Copy link

@Copilot 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

This PR enhances the ActiveField::label() method with a new tag option to control the wrapper element type and fixes how labelOptions work for checkbox/radio inputs when not enclosed by label. The enhancement maintains full backward compatibility while providing more flexibility for custom label rendering.

Key Changes:

  • Added tag option to label() method for customizing wrapper elements
  • Fixed labelOptions handling in checkbox/radio methods when enclosedByLabel is false
  • Added comprehensive test coverage for new functionality

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
framework/widgets/ActiveField.php Core implementation of tag option in label() method and refactored checkbox/radio label handling
tests/framework/widgets/ActiveFieldTest.php Comprehensive test coverage for new label functionality and improved test format
framework/UPGRADE.md Documentation of new feature for upgrade guide
framework/CHANGELOG.md Added entry for enhancement #20537
Comments suppressed due to low confidence (1)

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

public function label($label = null, $options = [])
{
if ($label === false) {
if ($label === false || (isset($this->labelOptions['label']) && $this->labelOptions['label'] === false)) {
Copy link
Preview

Copilot AI Sep 29, 2025

Choose a reason for hiding this comment

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

The condition check for $this->labelOptions['label'] === false seems unexpected here. The labelOptions array is typically for HTML attributes, not for controlling label visibility. This could create confusion and should be documented or reconsidered.

Suggested change
if ($label === false || (isset($this->labelOptions['label']) && $this->labelOptions['label'] === false)) {
if ($label === false) {

Copilot uses AI. Check for mistakes.

Copy link
Contributor

@xicond xicond Sep 30, 2025

Choose a reason for hiding this comment

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

I think user should allowed to overwrite the label with newer value, isn't it ?

If set to `false`, the label content will be rendered as raw HTML without any wrapper tag.

except it's null

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct the user can overwrite the label.

testLabelPriorityOfContent

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean

$this->activeField(['label'=>false])->label('anything');

newer should be the final decision

Copy link
Member Author

Choose a reason for hiding this comment

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

Update docs.

Copy link
Member Author

Choose a reason for hiding this comment

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

I mean

$this->activeField(['label'=>false])->label('anything');

newer should be the final decision

@samdark How would you treat this edge case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Label precedence.

  • $label parameter (highest priority).
  • options['label'] (only when $label === null).
  • Model attribute label (fallback).
  • If any of these equals false, the label won't render.

Copy link
Member

@Arhell Arhell left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link

@Copilot 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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

public function label($label = null, $options = [])
{
if ($label === false) {
if ($label === false || (isset($this->labelOptions['label']) && $this->labelOptions['label'] === false)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I mean

$this->activeField(['label'=>false])->label('anything');

newer should be the final decision

@Copilot Copilot AI review requested due to automatic review settings September 30, 2025 11:33
@terabytesoftw
Copy link
Member Author

@xicond

$this->activeField(['label'=>false])->label('anything'); done

Add more tests.

thks, for review 👍

Copy link

@Copilot 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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Copilot Copilot AI review requested due to automatic review settings September 30, 2025 11:47
Copy link

@Copilot 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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Copilot Copilot AI review requested due to automatic review settings September 30, 2025 12:01
Copy link

@Copilot 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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Copilot Copilot AI review requested due to automatic review settings October 1, 2025 16:03
Copy link

@Copilot 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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@terabytesoftw terabytesoftw marked this pull request as draft October 1, 2025 19:56
@terabytesoftw terabytesoftw marked this pull request as ready for review October 1, 2025 19:57
@Copilot Copilot AI review requested due to automatic review settings October 1, 2025 19:57
Copy link

@Copilot 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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

*/
protected function generateLabel(array $options): array
{
if (isset($options['label']) && $options['label'] !== false && $this->parts['{label}'] === '') {
Copy link
Preview

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider extracting the condition $this->parts['{label}'] === '' into a more descriptive variable or method name to improve readability, such as $isLabelEmpty or $this->isLabelEmpty().

Copilot uses AI. Check for mistakes.


unset($options['labelOptions']['tag']);

$this->labelOptions = array_merge($this->labelOptions, $options['labelOptions']);
Copy link
Preview

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

[nitpick] The labelOptions merging could overwrite existing options unintentionally. Consider using a more explicit merge strategy or documenting the precedence behavior to avoid unexpected side effects.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ActiveField: Enhance label() method with tag option and fix labelOptions for checkbox/radio.
4 participants