Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveDataProvider generates label values which are never used #19899

Closed
PowerGamer1 opened this issue Jul 18, 2023 · 0 comments
Closed

ActiveDataProvider generates label values which are never used #19899

PowerGamer1 opened this issue Jul 18, 2023 · 0 comments
Labels
status:ready for adoption Feel free to implement this issue. type:bug Bug
Milestone

Comments

@PowerGamer1
Copy link
Contributor

When GridView is based on ActiveDataProvider the Model::generateAttributeLabel() will be called even when the label is explicitly specified in GridView::columns. The correct label value (from GridView::columns) is rendered but the incorrect label value is still being generated and never used. In such situation automatic label generation should not happen because it is pointless and also interferes with user intention of raising an error instead of generating values for labels that are not explicitly defined (by throwing exception in overridden generateAttributeLabel() method).

What steps will reproduce the problem?

// Database table for this class has 2 attributes: "attr1" and "attr2".
class MyModel extends \yii\db\ActiveRecord
{
    public function attributeLabels() {
        return [
            'attr1' => 'Label for attr1',
            // Label for 'attr2' is NOT defined here
        ];
    }
    public function generateAttributeLabel($name) {
        throw new \yii\base\InvalidArgumentException('Label not defined!');
    }
}

$grid = new \yii\grid\GridView([
    'dataProvider' => new \yii\data\ActiveDataProvider([
        'query' => MyModel::find(),
    ]),
    'columns' => [
        'attr1',
        'attr2:text:Label for attr2', // Defines label for 'attr2'.
    ],
]);
$grid->renderTableHeader();

What is the expected result?

MyModel::generateAttributeLabel('attr2') IS NEVER CALLED and exception is not thrown.

What do you get instead?

MyModel::generateAttributeLabel('attr2') IS CALLED and throws exception.

Additional info

Q A
Yii version 2.0.48.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue. type:bug Bug
Projects
None yet
Development

No branches or pull requests

2 participants