Damon
Damon

Reputation: 4514

How can I include html within a form label using Laravel Collective?

Reading through this SO thread I have read that I can create a new macro to create custom form inputs.

I am brand new to Laravel development (big surprise) & it seems a tad overkill for such a small thing. Is there a "simpler" way to have something like this:

blade template

{!!Form::label('firstName', 'First Name<sup>*</sup>') !!}
{!! Form::text('firstName', null, ['class'=>'required']) !!} 

html

<label for="firstName">First Name*</label>
<input type="text" name="firstName" class="required">

Or, is this a case where I just write the html, and then use the form service to create the inputs?

Thank you for your patience and insight.

Upvotes: 14

Views: 10045

Answers (4)

Shaarif Mehmood
Shaarif Mehmood

Reputation: 301

{!! Html::decode(Form::label('email', 'E-Mail Address', ['class' => 'text-muted'])) !!}

that is much better to fix these type of problems

Upvotes: 1

Salikh Gurgenidze
Salikh Gurgenidze

Reputation: 634

The simple way to do this is

{!! Form::label('labelFor','labelText',[],false) !!} 

the last parameter is $escape_html which default value is "true".

Upvotes: 34

Maybe it's late to answer, but you could do this:

{!! Html::decode(Form::label('firstName','FirstName: <sup>*</sup>')) !!}
{!! Form::text('firstName', null, ['class'=>'required']) !!}

Upvotes: -1

Tomas Buteler
Tomas Buteler

Reputation: 4117

The Form class attributes will always be escaped (they were in Laravel 4 and still are with Laravel Collective's 5+ support), therefore no HTML is allowed. Since you're needs are so simple, I'd suggest just writing plain HTML.

If you want overkill, maybe something like this in your AppServiceProvider.php:

Form::macro('labelWithHTML', function ($name, $html) {
    return '<label for="'.$name.'">'.$html.'</label>';
});

Then, in your Blade templates:

{!! Form::labelWithHTML('firstName', 'First Name<sup>*</sup>') !!}
{!! Form::text('firstName', null, ['class'=>'required']) !!} 

Upvotes: 5

Related Questions