2013-10-28 2 views
2

내가 원하는 것을 할 수 있습니까? add method의 세 번째 매개 변수는 같은 predifined 옵션의 배열 인양식 작성 도구 추가 방법에 대한 사용자 지정 옵션 만들기

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

:

내가 어떻게 양식 필드를 만들 것을 알고 label, attr, 등등 ... 당신은 같은 것을 할 경우 :

$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 메서드의 옵션을 전달하고 싶지 않습니다.

내가 원하는 것은 에 세 번째 매개 변수의 배열에 대한 사용자 지정 option을 만드는 것입니다.

죄송합니다.

답변

3

문제를 해결했습니다. hcoat의 코멘트 @ 답변을 모든

첫째, 내가 form theming에 대한 3 custom options (open_col, close_col, col_dims) 갖고 싶어.

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

을 그리고이 같은 그 옵션 값 검색 : 나는 attr 옵션에서 그들을 통과 그것은 잘 작동

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

을 그리고 당신은 나뭇 가지 템플릿에서이 속성을 얻을 수 :

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