Pregunta

Is it possible to do what i want ?

I know that how to create a form field :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('field', null, array_of_options)
    ;
}

The third parameter of add method is an array of predifined options like :label, attr, etc... and if you do something like this :

$builder
    ->add('field', null, array('my_option' => 'my value'));

you will get this error :

The option "my_option" does not exist. Known options are: "action", "attr", "auto_initialize", "block_name", "by_reference", "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "grouping", "inherit_data", "intention", "invalid_message", "invalid_message_parameters", "label", "label_attr", "mapped", "max_length", "method", "pattern", "post_max_size_message", "precision", "property_path", "read_only", "required", "rounding_mode", "translation_domain", "trim", "validation_groups", "virtual" 

I have read and understood this but it's not what i m looking for. I don't want to pass options in createForm method from controller.

What i want is to create a custom option for the array of the third parameter in add method.

Sorry if i m not clear !

¿Fue útil?

Solución

I have solved the issue.

First of all, to answer @hcoat's comment, i want to have 3 custom options (open_col, close_col, col_dims) for form theming. I passed them in the attr option:

 public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('field1', null, array('attr' => array('open_col' => true, 'col_dims' => '2-8')))
        ->add('field2', null, array('attr' => array('close_col' => true, 'col_dims' => '6-8')))
    ;
}

And retrieved those option values like this :

{% block form_row %}
{% spaceless %}



{% set open_col, close_col = 'open_col', 'close_col' %}
    {% if open_col in attr|keys %}
    <div class="mws-form-row">
        <div class="mws-form-cols">
            <div class="mws-form-col-{{ (open_col in attr|keys) ? attr['col_dims']:'4-8' }}">
    {% elseif close_col in attr|keys  %}
            <div class="mws-form-col-{{ (open_col in attr|keys) ? attr['col_dims']:'4-8' }}">
    {% else %}
    <div class="mws-form-row">
    {% endif %}

        {{ form_label(form) }}
        <div class="mws-form-item">
            {{ form_widget(form) }}
        </div>
    {% if close_col in attr|keys %}
            </div>
        </div>
    </div>
    {% elseif open_col in attr|keys  %}
    </div>
    {% else %}
    </div>
    {% endif %}
{% endspaceless %}
{% endblock form_row %}

It works fine !

Otros consejos

I think that your solution isnt perfect. Of course it work but you should consider another solution.

You should use options resolver which allow you to add your own property. http://symfony.com/doc/current/components/options_resolver.html

So in your formType class you should add following method:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(
        array(
            'my_option' => 'my_default_value',
        )
    );
}

And then you can get this property in twig template as:

  {{ my_option }} //it return"my_defaul_value"
  {{ form.your_field_name.my_option }} //it retun your field value 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top