35

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?

userlond
  • 3,632
  • 2
  • 36
  • 53
Joshi
  • 2,730
  • 5
  • 36
  • 62

7 Answers7

79

Ok, I found the solution.

<?= $form->field($model, 'sample_text')->textArea()->label(false) ?>
Joshi
  • 2,730
  • 5
  • 36
  • 62
7

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>
trejder
  • 17,148
  • 27
  • 124
  • 216
7

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)
?>
ajmedway
  • 1,492
  • 14
  • 28
  • 1
    I'm surprised why your answer isn't marked as the best. – Hit-or-miss Jul 01 '19 at 09:14
  • It should be. OP @joshi, please consider as this will help others to see the best current solution – ajmedway Oct 05 '20 at 15:40
  • 1
    Better than the actual yii documentation and dotblock suggestions. This solution allows for validation of the hidden input correctly as part of the active form. Would you believe instead of using label(false) they suggest using Html::activeHiddenInput() – Fi Horan Nov 10 '20 at 15:30
5
<?= $form->field($model, 'password', [
    'inputOptions'=>[
        'class'=>'form-control',
        'placeholder'=>'Password'
    ]
])->passwordInput()->label(false); ?>
trejder
  • 17,148
  • 27
  • 124
  • 216
  • Throwing just a piece of code, badly formatted (fixed) isn't a good start here. – trejder Apr 14 '15 at 10:13
  • @HelgaIliashenko No, it is not. This site is not a bunch of working answers. This is a world most famous Q&A site, that has its own rules. One of these rules is to provide an comprehensive answer, not a copy-pasted piece of code. Voting up answers that directly violates one of the most important rules here clearly is a false shot. – trejder Nov 20 '16 at 13:51
  • @HelgaIliashenko You're part of this community and your opinion matters here -- of course. But, when your opinion supports a style of writing answers that is directly forbidden (or at least pointed out as very not good way of writing good answers) then I feel responsible for pointing this out to you. This particular solution works for you, but will not work (without proper textual explanation) for others, maybe less experienced than you. And all SE users should tend to have answers here that are good for _all_, not just for _some_. That's my opinion. You're welcome to disagree with it. – trejder Dec 04 '16 at 08:59
4
<?= $sffForm->field($sffModel, 'url_keywords', ['enableLabel' => false])->textInput(['placeholder' => 'URL / keywords']) ?>
gvanto
  • 1,936
  • 3
  • 21
  • 26
  • 1
    While this code snippet may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Eel Lee Jun 02 '17 at 14:31
  • This removes the offset of extra space as well – Saurabh Jul 13 '17 at 05:06
0

You can disable label, while creating form field class

$form->field($model, 'email', [
 'inputOptions' => [
    'enableLabel' => false,
  ]
 ])   
s_mart
  • 735
  • 7
  • 21
  • 2
    Does not work because it was changed to `= $form->field($formImage, 'fileUrl', ['enableLabel' => false])->textInput(); ?>` – user1954544 Oct 12 '16 at 10:34
-3

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' => '',
        .
        .
        .
          ]
 }
  • That doesn't remove the label. Just hides doesn't display a text. But the label element is still there, taking space. – MEM Apr 29 '15 at 13:53
  • You are right, it only hide the label but that was the original question asked. 'Hide label for input field', so it did that, hide the input field label. – Abdur Rahman Warriach Apr 30 '15 at 07:04