Hide label for input field

2019-03-17 05:34发布

I am trying to hide the label for a specific field in _form.php without success.

I have tried couple of variation like, but none is working:

<?= $form->field($model, 'sample_text')->textArea('label'=>false) ?>

and alternate code:

<?= $form->field($model, 'sample_text')->textArea('label'=>'') ?>

What is the right approach to hide a label?

7条回答
做个烂人
2楼-- · 2019-03-17 06:06

The best way to hide the label in form input field, is to pass empty value to array on 'attributeLabels()' function in the model.

i.e you have input filed name 'client_name', so on the 'attributeLabels()' function's return array pass the empty string as array value

public function attributeLabels()
{
    return [

        'id' => 'ID',
        'gender' => 'Gender',
        'client_name' => '',
        .
        .
        .
          ]
 }
查看更多
Evening l夕情丶
3楼-- · 2019-03-17 06:14

Or you can modify template value for particular field and remove {label} part from it. I.e.:

<p><?= $form->field($page, 'image', [
    'template' => '<div class=\"\">{input}</div><div class=\"\">{error}</div>'
])->fileInput() ?></p>
查看更多
Juvenile、少年°
4楼-- · 2019-03-17 06:14

You can disable label, while creating form field class

$form->field($model, 'email', [
 'inputOptions' => [
    'enableLabel' => false,
  ]
 ])   
查看更多
Rolldiameter
5楼-- · 2019-03-17 06:15
<?= $form->field($model, 'password', [
    'inputOptions'=>[
        'class'=>'form-control',
        'placeholder'=>'Password'
    ]
])->passwordInput()->label(false); ?>
查看更多
smile是对你的礼貌
6楼-- · 2019-03-17 06:21

Ok, I found the solution.

<?= $form->field($model, 'sample_text')->textArea()->label(false) ?>
查看更多
趁早两清
7楼-- · 2019-03-17 06:21

At time of writing after digging into the core code, I have found this to be the best solution to hide the label and prevent rendering the full field template with errors etc. for hiddenInput.

<?=
$form->field($model, 'address_uuid', [
    'template' => '{input}',
    'options' => ['tag' => false]
])->hiddenInput([
    'readonly' => true,
])->label(false)
?>
查看更多
登录 后发表回答