php - How to create form twig templates for a custom FormType? -
given symfony 2.8 project, in form want use more complex type consists of 4 text-input-fields (the reasons user input of opening times per week day).
my question:
how can define custom twig template type having multiple form-elements in it? how symfony know, template belongs type? read docs, mismatching labels , names don't fit together.
what i've done far:
i created following type:
/** * class openingtype */ class openingtype extends abstracttype { /** * @param formbuilderinterface $builder * @param array $options */ public function buildform(formbuilderinterface $builder, array $options) { $builder->add('from', 'text', [ 'label' => '', 'attr' => ['placeholder' => '08:00'] ]); $builder->add('until', 'text', [ 'label' => '', 'attr' => ['placeholder' => '18:00'] ]); } }
for first try, added 2 text fields here.
the type registered service:
example.form.type.opening: class: example\mainbundle\form\type\openingtype tags: - { name: form.type }
... , added way main form:
->add('opening_monday', openingtype::class, ['label' => 'monday'])
how achieve use custom widget twig template custom type?
here's link in doc customize theming : http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field or http://symfony.com/doc/current/cookbook/form/form_customization.html#twig
what can "locally":
{# /views/controller/action.html.twig #} {% block _{ form prefix }_opening_monday_row %} {# customize theme here #} {% endblock %} {% form_theme form _self %} ... {{ form_row(form.opening_monday) }}
the prefix default form name 'form' , field name, here 'open_monday', can customize in form builder :
$builder->add('opening_monday', openingtype::class, ['label' => 'monday', 'block_name' => 'custom_prefix'])
or globally in app/config.yml
:
twig: form_themes: - form/custom_fields.html.twig
and in app/resources/views/form/custom_fields.html.twig
:
{% block custom_type_name_raw %} {# customise "form_row" here #} {% endblock %} {% block custom_type_name_label %} {# customise "form_label" here #} {% endblock %} {% block custom_type_name_widget %} {# customise "form_widget" here #} {% endblock %}
needs following method in customformtype
:
// symfony 2.8+ public function getblockprefix() { return "custom_type_name"; } // before 2.7 included public function getname() { return "custom_type_name"; }
Comments
Post a Comment