Reputation: 111
I have created Custom Form Field Type according to the documentation and it works fine(I mean no errors). I have a class:
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class RangeType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
));
}
public function getParent()
{
return RangeType::class;
}
}
I have a twig template:
{# app/Resources/views/form/fields.html.twig #}
{% block range_widget %}
<div class="cell-md-12 offset-top-45 offset-md-top-0">
<div data-min="" data-max="6000" data-start="[9, 2999]" data-step="10" data-tooltip="false" data-min-diff="10" data-input=".rd-range-input-value-2" data-input-2=".rd-range-input-value-3" class="rd-range"></div>
<hr class="divider divider-offset-lg divider-gray veil reveal-md-block">
</div>
{% endblock %}
I added that to the config:
# app/config/config.yml
twig:
form_themes:
- 'form/fields.html.twig'
and finally I have a code to add the field to the form:
$builder->add('price', RangeType::class, [
'data' => $options['priceRange'] ? $options['priceRange'] : $options['minPrice'].",".$options['maxPrice'],
'attr'=> [
'data-min' => $options['minPrice'],
'data-max' => $options['maxPrice'],
'data-dimension' => $options['dimension']
]
]);
How do I use that "data", "data-min", "data-max", "data-dimension" options from the form builder in the twig template? So far as you can see all of them are static in the template.
Upvotes: 2
Views: 2812
Reputation: 81
You can access you 'attr' option values by using the folowing path:
form.vars.attr
So, to get the 'data-min' parameter value form example, you'll just need to do:
{% set attr = form.vars.attr %}
<div data-min="{{ attr['data-min'] }}" ...
Upvotes: 2