Jakub Matczak
Jakub Matczak

Reputation: 15696

How to put HTML into button's content?

What I want to achieve is to render a button with view helpers (form_row etc.), but I also use FontAwesome icons, so I need to put some HTML into button's content.

The problem is that special chars are converted to HTML entities, so it doesn't behave as HTML anymore.

What I want:

 <button type="button" name="form[save]"><i class="fa fa-sign-in"></i> Sign in</button>

What I get:

 <button type="button" name="form[save]">\&lt;i class=&quot;fa fa-sign-in&quot;&gt;&lt;/i&gt; Sign in</button>

What I'm doing:

I create a form like this:

$form = $this->createFormBuilder($user)
    // other fields...              
    ->add('save', 'submit', array('label' => '<i class="fa fa-sign-in"></i> Sign in'))
    ->getForm();

And I render this button like this:

{{form_row(regForm.save)}}

Is it possible to prevent encoding to HTML entites?

Update: The cleanest solution I found since now is to use raw filter while overriding button_row block template. Is it the best way to do this?

Upvotes: 0

Views: 625

Answers (1)

gp_sflover
gp_sflover

Reputation: 3500

If you are using {{form_row(regForm.save)}} to render the button is more simple to render it directly in the view like this:

<button type="submit" class="btn green"><i class="fa fa-sign-in"></i> Sign in {{ 'form.global.button.sign_in'|trans({}, 'form') }}</button>

otherwise you can create a button and/or icon twig extension (like did in MopaBootstrapBundle) to implement it in the form component (and to reuse it everywhere).

Upvotes: 1

Related Questions