egeloen
egeloen

Reputation: 5864

How can I set the template which is rendered with a custom form field type?

I'm actually developping a custom form field type which extends a file form field type. The documentation about that has not been written yet.

I have created my custom field & register it like service.

Custom form field type:

class CustomFileType extends AbstractType
{
    public function getParent(array $options)
    {
        return 'file';
    }

    public function getName()
    {
        return 'custom_file';
    }
}

Service:

<service id="form.type.custom_file" class="Namespace\CustomFileType">
    <tag name="form.type" alias="custom_file" />
</service>

I can use it like a file field but I don't know how can I set the template which will be render with my custom field.

Upvotes: 2

Views: 436

Answers (1)

futurecat
futurecat

Reputation: 858

In the template rendering the form, you can include this:

{% form_theme form _self %}

{% block custom_file_widget %}
  {# rendering of the widget goes here #}
{% endblock %}

Check how the form_div_layout.html.twig file is made and you'll understand the underlying mechanism.

Upvotes: 1

Related Questions