yii\widgets\ActiveField::label PHP Method

label() public method

Generates a label tag for [[attribute]].
public label ( null | string | false $label = null, null | array $options = [] )
$label null | string | false the label to use. If `null`, the label will be generated via [[Model::getAttributeLabel()]]. If `false`, the generated field will not contain the label part. Note that this will NOT be [[Html::encode()|encoded]].
$options null | array the tag options in terms of name-value pairs. It will be merged with [[labelOptions]]. The options will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. If a value is `null`, the corresponding attribute will not be rendered.
    public function label($label = null, $options = [])
    {
        if ($label === false) {
            $this->parts['{label}'] = '';
            return $this;
        }
        $options = array_merge($this->labelOptions, $options);
        if ($label !== null) {
            $options['label'] = $label;
        }
        if ($this->_skipLabelFor) {
            $options['for'] = null;
        }
        $this->parts['{label}'] = Html::activeLabel($this->model, $this->attribute, $options);
        return $this;
    }

Usage Example

    public function testError()
    {
        $expectedValue = '<label class="control-label" for="dynamicmodel-attributename">Attribute Name</label>';
        $this->activeField->label();
        $this->assertEquals($expectedValue, $this->activeField->parts['{label}']);
        // label = false
        $expectedValue = '';
        $this->activeField->label(false);
        $this->assertEquals($expectedValue, $this->activeField->parts['{label}']);
        // $label = 'Label Name'
        $label = 'Label Name';
        $expectedValue = <<<EOT
<label class="control-label" for="dynamicmodel-attributename">{$label}</label>
EOT;
        $this->activeField->label($label);
        $this->assertEquals($expectedValue, $this->activeField->parts['{label}']);
    }
All Usage Examples Of yii\widgets\ActiveField::label