【问题标题】:create custom options for form builder add method为表单构建器添加方法创建自定义选项
【发布时间】:2013-10-28 01:47:58
【问题描述】:

有没有可能做我想做的事?

我知道如何创建表单域:

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

add method 的第三个参数是一个预定义选项数组,例如:labelattr 等……如果你这样做:

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

你会得到这个错误:

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" 

我已经阅读并理解了this,但这不是我想要的。我不想从控制器传递createForm 方法中的选项。

我想要的是为add method中的第三个参数的数组创建一个自定义option

如果我不清楚,对不起!

【问题讨论】:

    标签: symfony formbuilder


    【解决方案1】:

    我已经解决了这个问题。

    首先,为了回答@hcoat 的评论,我希望form theming 有3 个custom optionsopen_colclose_colcol_dims)。我在attr 选项中传递了它们:

     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')))
        ;
    }
    

    并像这样检索那些选项值:

    {% 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 %}
    

    效果很好!

    【讨论】:

      【解决方案2】:

      我认为您的解决方案并不完美。当然可以,但您应该考虑另一种解决方案。

      您应该使用允许您添加自己的属性的选项解析器。 http://symfony.com/doc/current/components/options_resolver.html

      所以在你的 formType 类中你应该添加以下方法:

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

      然后你可以在 twig 模板中获取这个属性:

        {{ my_option }} //it return"my_defaul_value"
        {{ form.your_field_name.my_option }} //it retun your field value 
      

      【讨论】:

        猜你喜欢
        • 2015-04-07
        • 1970-01-01
        • 2021-06-10
        • 1970-01-01
        • 2017-03-17
        • 1970-01-01
        • 2015-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多