program story

클래스 속성을 Symfony2 양식 입력으로 설정하는 방법

inputbox 2020. 11. 22. 19:27
반응형

클래스 속성을 Symfony2 양식 입력으로 설정하는 방법


in을 사용하여 HTML class속성을 양식에 어떻게 설정할 수 있습니까?<input>FormBuilderSymfony2

이 같은:

->add('birthdate', 'date',array(
      'input' => 'datetime',
      'widget' => 'single_text',
      'attr' => array(
          'class' => 'calendar'
      )
 ))

 {{ form_widget(form.birthdate) }}

input필드 class달력으로 설정 한 속성을 원합니다.


나뭇 가지 템플릿에서이 작업을 수행 할 수 있습니다.

{{ form_widget(form.birthdate, { 'attr': {'class': 'calendar'} }) }}

에서 http://symfony.com/doc/current/book/forms.html#rendering-each-field-by-hand


FormBuilder로 할 수 있습니다. FormBuilder의 배열에 다음을 추가하십시오.

'attr'=> array('class'=>'span2')

Acyra의 대답은 컨트롤러 내부에 속성을 설정하고 싶지만 많은 부정확성이있는 경우 올바른 방향으로 안내합니다.

예, 다음과 같이 옵션 배열에 attr속성 ( 2.1 버전의 경우 여기에, 2.0의 경우 여기도입 됨)을 사용하여 FormBuilder로 직접 수행 할 수 있습니다 .

->add('birthdate', 'date',array(
      'input' => 'datetime',
      'widget' => 'single_text',
      'attr' => array('class'=>'calendar')
 ))

"기능이 망가졌다"는 것은 사실이 아닙니다. 아주 잘 작동합니다!

Symfony2 class가 레이블과 입력 (적어도 2.1 버전부터) 모두에 HTML 속성을 적용하는 것은 사실이 아닙니다 .

또한 attr속성 자체가 배열 이기 때문에 필드에 대해 렌더링하려는 HTML 속성을 전달할 수 있습니다. HTML5 data-속성을 전달하려는 경우 매우 유용 합니다.


양식 클래스의 옵션에 추가 할 수 있습니다.

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\MyEntity',
        'attr' => array(
            'class' => 'form-horizontal'
        )
    ));
}

{{ form_widget(form.content, { 'attr': {'class': 'tinyMCE', 'data-theme': 'advanced'} })  }}

이렇게 :

{{ form_widget(form.description, { 'attr': {'class': 'form-control', 'rows': '5', 'style': 'resize:none;'} }) }}

위의 예에 표시된대로 Twig 또는 FormClass에서이를 수행 할 수 있습니다. 하지만 컨트롤러에서 폼이 어떤 클래스를 가져와야하는지 결정할 수 있습니다. 일반적으로 컨트롤러에 많은 로직이 없다는 것을 명심하십시오!

    $form = $this->createForm(ContactForm::class, null, [
        'attr' => [
            'class' => 'my_contact_form'
        ]
    ]);

Renders the HTML widget of a given field. If you apply this to an entire form or collection of fields, each underlying form row will be rendered.

{# render a field row, but display a label with text "foo" #} {{ form_row(form.name, {'label': 'foo'}) }}

The second argument to form_row() is an array of variables. The templates provided in Symfony only allow to override the label as shown in the example above.

See "More about Form Variables" to learn about the variables argument.

참고URL : https://stackoverflow.com/questions/6734821/how-to-set-a-class-attribute-to-a-symfony2-form-input

반응형