Reputation: 949
UPDATE/EDIT
This is what I have done, but for some reason its not working. So the services is as follows,
clearfix.extended_type:
class: Bundle\Form\ClearfixExtension
tags:
- { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\FormType }
Now I am sure this is wrong, I have not used services much and still trying to get my head around them.
I ave also built the class, see answer below for anyone else reading this, with the right namespace.
I have also done the following to the block,
{%- block form_row -%}
<div class="other classes here {% if clearfix %}clearfix"{% else %}"{% endif %}>
{{- form_widget(form) -}}
{{- form_label(form) -}}
{{- form_errors(form) -}}
</div>
{%- endblock form_row -%}
But all I get from my form when I try to render it is, "Variable "clearfix" does not exist in....."
So what have I done wrong? I guessing it has something to do with the service setup?
Thanks
ok, this is doing my head in a little, has I have gone over and over the docs but I am either doing it completely wrong or its what I want to do is just not do able?
So I am using Symfony 2.7, and have it setup to point to a forms twig file, so that I can override some of the defaults. This is working fine without any problems. However I now want to be able to render some of the rows with an added clearfix class. This is what I am rending right now,
{%- block form_row -%}
<div>
{{- form_widget(form) -}}
{{- form_label(form) -}}
{{- form_errors(form) -}}
</div>
{%- endblock form_row -%}
That renders all my rows with the right classes, but I want to be able to use the following on some selected rows
{%- block _clerfix -%}
<div class="clearfix">
{{- form_widget(form) -}}
{{- form_label(form) -}}
{{- form_errors(form) -}}
</div>
{%- endblock _clerfix -%}
And then I call this with the block_name
within the Abstract Type of the form that I want to build with the added clearfix class, like so,
->add('password', 'password', ['label' => 'Password',
'required' => false,
'block_name' => 'clearfix',
'attr' => ['autocomplete' => 'off','class' => 'Passwordd', 'aria-required' => 'true' ]
])
Is what I want doable? I don't really want to add that clearfix into the user form I am building now, as that will not keep my code very DRY, as there will be (one would think so) places where I will need to re-use that block to render other fields?
All help most welcome.
Thanks.
Upvotes: 0
Views: 205
Reputation: 2889
You're can create form extension for defining option to apply clearfix
Symfony 2.8
class ClearfixExtension extends AbstractTypeExtension
{
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['clearfix'] = $options['clearfix'];
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('clearfix', false);
}
public function getExtendedType()
{
return FormType::class;
}
}
services.yml
clearfix_extension:
class: Bundle\Form\ClearfixExtension
tags:
- { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\FormType }
Symfony 2.7
class ClearfixExtension extends AbstractTypeExtension
{
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['clearfix'] = $options['clearfix'];
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('clearfix', false);
}
public function getExtendedType()
{
return 'form';
}
}
services.yml
clearfix_extension:
class: Bundle\Form\ClearfixExtension
tags:
- { name: form.type_extension, alias: form }
and check this options in twig
{%- block form_row -%}
<div {% if clearfix %}class="clearfix"{% endif %}>
{{- form_widget(form) -}}
{{- form_label(form) -}}
{{- form_errors(form) -}}
</div>
{%- endblock form_row -%}
Upvotes: 2