7

Is there some easy way how put raw HTML tag to label? I have this:

{{ Form::label('firstName', 'First name <em>*</em>', array('class' => 'input_tag')) }}

and it produces:

<label class="input_tag" for="firstName">First Name &lt;em&gt;*&lt;/em&gt;</label>

BUT tag EM is not interpreted as it should be. What I want is:

<label class="input_tag" for="firstName">First Name <em>*</em></label>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Lajdák Marek
  • 2,969
  • 8
  • 29
  • 58

7 Answers7

13

use HTML::decode():

{!! HTML::decode(Form::label('firstName', 'First name <em>*</em>', array('class' => 'input_tag'))) !!}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Aureliano Far Suau
  • 6,531
  • 2
  • 22
  • 25
  • 3
    That would work in this specific example, but do be aware that there may be other entities in the resulting label element that you don't want decoded. – Jason Sep 30 '13 at 10:28
  • 1
    This worked like a charm. You may need to use the {!! !!} instead of the {{ }}. Thanks for the tip! – cbloss793 May 03 '17 at 17:29
12

Using sprintf in a macro is much faster than redecoding:

Form::macro('rawLabel', function($name, $value = null, $options = array())
{
    $label = Form::label($name, '%s', $options);

    return sprintf($label, $value);
});
Adam Rivers
  • 1,065
  • 7
  • 13
  • 4
    If you only need this once and you want a quick and easy solution, you can do it inline too, e.g. `{{ sprintf(Form::label('tncs', '%s', array('class' => 'control-label tncs-label')), 'I agree with the terms and conditions') }}` – neilcrookes Dec 19 '13 at 10:25
  • Yes, that would work, but even for 1 use solutions its still better to extract it out. It's ugly and difficult to understand. – Adam Rivers Dec 20 '13 at 11:50
  • Where would the appropriate location be for this "widget" if I want to use it in multiple Views? I would prefer to include it only once, not every time I will need it. – Kristjan O. Jan 14 '15 at 17:16
  • I would have placed it in the boot function of your AppServiceProvider. You will need to reference the full FormFacade and not its alias (e.g. `\Collective\Html\FormFacade::` not `Form::`) – Matt McDonald Apr 21 '16 at 09:28
1

You can create a helper function (macro) just like:

HTML::macro('raw', function($htmlbuilder) {
   return htmlspecialchars_decode($htmlbuilder);
});

in your view:

{{ HTML::raw(Form::label('input_firstname', 'Prénom <sup>*</sup>')) }}
Youssef Lahssini
  • 121
  • 4
  • 11
0

I often use raw html for form input and labels as I find it easier to read and use html5 attributes such as required and placeholder. This form is a good example of how to use raw html with Input::old which allows you to capture old input. https://github.com/Zizaco/confide/blob/master/src/views/login.blade.php

Thomas Clarkson
  • 755
  • 9
  • 11
0

For required inputs, instead of trying to add HTML into the label, I add a class, 'required-input' (or something).

Then I have the following CSS rule

label.required-input:before {
    content: ' * ';
    color: #f00;
}

This would work unless you are needing to have the <em>*</em> for screen readers. But you can still add the required flag on the input.

erj1
  • 79
  • 3
-1

Got it, it's using the e() helper function that uses htmlentities() to format your labels and this is converting your tag to &amp;lt;em&amp;gt;*&amp;lt;/em&amp;gt;.

The quick and (very) dirty fix is to add this to your start.php or some other place before the first call to Helpers.php:

function e($value) { return $value; }

But this far from ideal.

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
-2

I believe the Form::label($name, $value, $attributes, $escape_html) takes a fourth parameter which tells it whether to not to escape html. It defaults to true. So if your expected result is an italic *, pass false as the fourth parameter.

  • 1
    Looking at the API documentation for Form::label, we can see that there is no fourth parameter to escape html. http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#171-188 – erj1 Aug 22 '13 at 19:12