代码之家  ›  专栏  ›  技术社区  ›  Andrei Herford

Symfony自定义fom小部件:如何自动将属性添加到特定的表单字段(而不是完整的小部件)?

  •  0
  • Andrei Herford  · 技术社区  · 5 年前

    jQueryUI 如何向这些输入控件添加属性?


    细节:

    假设以下自定义类应由自定义小部件表示:

    class Room {
        public $building = "main";
        public $roomNo = 1;
    }
    
    class Lecture {
        public $date;
        public $room;
    }
    
    class LectureType extends AbstractType {
        ...
        public function buildForm(FormBuilderInterface $builder, array $options) {
            $builder
                ->add('date', DateType::class, [...])            
                ->add('room', RoomType::class, [...])   // Using custom RoomType
            ;
        }
    }  
    
    class RoomType extends AbstractType {
        ...
        public function buildForm(FormBuilderInterface $builder, array $options) {
            $builder
                ->add('building', ChoiceType::class, [...])            
                ->add('roomNo', IntegerType::class, [...])   
            ;
        }
    }
    
    // room_widget.html.twig
    {% block app_room_widget %}
    
    {# Adding the attributes for the widget container or the complete widget is easy #}
    {# using either the widget_container_attributes or widget_attributes block. But  #}
    {# how to add the attributes to the specific fields?                             #}
    
    <div {{ block('widget_container_attributes') }} ">  
        ...
        <input {{ block('how_to_add_attributes_here?') }} />
    
        <input id="{{ form.building.vars.id }}" name="{{ form.value.building.full_name }}"{% if disabled %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if form.building.vars.value is not empty %} value="{{ form.building.vars.value }}"{% endif %}/>
    
        <script>
           // configure jQueryUI controls...
        </script>
    </div>
    {% endblock %}
    

    0 回复  |  直到 5 年前