Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony-Twig: insert fontawesome icon in a form_widget

To validate a form I am using a standard:

{{ form_widget(form.save, {'attr': {'class': 'btn btn-sm btn-danger'}, 'label': 'Submit form'}) }}

I want to insert a fontawsome icon in the button. I tried:

{{ form_widget(form.save, {'attr': {'class': 'btn btn-sm btn-danger'}, 'label': '<i class="fa fa-envelope-o"></i> Submit form'}) }}

But it is not working; obviously

Any idea how to that?

like image 908
Raphael_b Avatar asked Jul 07 '15 16:07

Raphael_b


People also ask

What is form_row () in Symfony?

Renders the "row" of a given field, which is the combination of the field's label, errors and widget. The second argument to form_row () is an array of variables. The templates provided in Symfony only allow to override the label as shown in the example above. See " More about Form Variables " to learn about the variables argument.

What is the second argument to form_row() in twig?

The second argument to form_row () is an array of variables. The templates provided in Symfony only allow to override the label as shown in the example above. See "Twig Template Form Function and Variable Reference" to learn about the variables argument. This renders all fields that have not yet been rendered for the given form.

Can I override the label in Symfony templates?

The templates provided in Symfony only allow to override the label as shown in the example above. See " More about Form Variables " to learn about the variables argument.

What is the second argument to form_ widget()?

The second argument to form_widget () is an array of variables. The most common variable is attr, which is an array of HTML attributes to apply to the HTML widget. In some cases, certain types also have other template-related options that can be passed. These are discussed on a type-by-type basis.


1 Answers

You can just add a new custom service css class per icon

/* 
 * css selector for a class attribute that starts with "btn-fa-" or has " btn-fa-" in it:
 */
[class^="btn-fa-"]:before,
[class*=" btn-fa-"]:before
{
    font-family: "Font Awesome 5 Free";
    font-weight: bold;
    margin: 0 6px 0 2px;
}

/*
 * And then only 1 setting per font awesome class
 */
.btn-fa-plus:before {
    content: '\f067';
}

And add the class to the ButtonType

->add('Add an item', ButtonType::class, [
    'attr' => [
        'class' => 'btn btn-primary btn-fa-plus',
    ]
])
like image 151
Julesezaar Avatar answered Sep 21 '22 13:09

Julesezaar