Symfony + bootstrap自定义表单

时间:2016-04-12 14:23:58

标签: twitter-bootstrap twitter-bootstrap-3 symfony

我正在尝试使用bootstrap为symfony表单创建自定义主题。 最终结果如下image

问题:

  • 带有令牌的按钮和输入不在表单中(在下面的html代码中显示) 为什么以及如何解决它?

  • 当我尝试在主窗体中添加2个dropzone表单时发生了奇怪的故障。第一种形式消失了。所以我需要在第一次表单之前填写空表格。

  • 是否可以在symfony中使用复选框制作droplist? "简单方法"对我来说是从客户端向服务器发出ajax请求, 获取数据,将数据放入droplist,而不是使用datatransformer droplist输入。是否真的有简单的方法来制作类似的东西 this

enter image description here

浏览器呈现页面时的Html代码:

<div class="container">
<form name="quest" method="post" class="form-horizontal">
...
</form>

<div><button type="submit" id="quest_save" name="quest[save]">Отправить</button></div>

<input value="tokenpart_W2u3ooA5-G0-tokenpart" id="quest__token" name="quest[_token]" type="hidden">
</div>

html.twig

<div class="container">
        {{ form_start(form, {'attr': {'class': 'form-horizontal'}}) }}
        {{ form_errors(form) }}
        <div class="form-group">
            {{ form_label(form.name, null, {'label_attr': {'class': 'col-sm-2 control-label'}}) }}
            <div class="col-sm-10">
                {{ form_errors(form.name) }}
                {{ form_widget(form.name, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>
        <div class="form-group">
            {{ form_label(form.description, null,{'label_attr': {'class': 'col-sm-2 control-label'}}) }}
            <div class="col-sm-10">
                {{ form_errors(form.description) }}
                {{ form_widget(form.description, {'attr': {'class': 'form-control','rows':'3'}}) }}
            </div>
        </div>
        <div class="form-group">
            {{ form_label(form.city, null,{'label_attr': {'class': 'col-sm-2 control-label'}}) }}
            <div class="col-sm-10">
                {{ form_errors(form.city) }}
                {{ form_widget(form.city, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>
        <div class="form-group">
            {{ form_label(form.organizer, null,{'label_attr': {'class': 'col-sm-2 control-label'}}) }}
            <div class="col-sm-10">
                {{ form_errors(form.organizer) }}
                {{ form_widget(form.organizer, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>
        <div class="form-group">
            {{ form_label(form.address, null,{'label_attr': {'class': 'col-sm-2 control-label'}}) }}
            <div class="col-sm-10">
                {{ form_errors(form.address) }}
                {{ form_widget(form.address, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>
        <div class="form-group">
            {{ form_label(form.metro, null,{'label_attr': {'class': 'col-sm-2 control-label'}}) }}
            <div class="col-sm-10">
                {{ form_errors(form.metro) }}
                {{ form_widget(form.metro, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>
        <div class="form-group">
            {{ form_label(form.phone, null,{'label_attr': {'class': 'col-sm-2 control-label'}}) }}
            <div class="col-sm-10">
                {{ form_errors(form.phone) }}
                {{ form_widget(form.phone, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>
        <div class="form-group">
            {{ form_label(form.mail, null,{'label_attr': {'class': 'col-sm-2 control-label'}}) }}
            <div class="col-sm-10">
                {{ form_errors(form.mail) }}
                {{ form_widget(form.mail, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">Цена</label>
            {{ form_label(form.minprice, null,{'label_attr': {'class': 'col-sm-2 control-label'}}) }}
            <div class="col-sm-2">
                {{ form_errors(form.minprice) }}
                {{ form_widget(form.minprice, {'attr': {'class': 'form-control'}}) }}
            </div>
            {{ form_label(form.maxprice, null,{'label_attr': {'class': 'col-sm-2 control-label'}}) }}
            <div class="col-sm-2">
                {{ form_errors(form.maxprice) }}
                {{ form_widget(form.maxprice, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div><!--/form-group-->
        <div class="form-group">
            <label class="col-sm-2 control-label">Кол-во игроков</label>
            {{ form_label(form.minplayers, null,{'label_attr': {'class': 'col-sm-2 control-label'}}) }}
            <div class="col-sm-2">
                {{ form_errors(form.minplayers) }}
                {{ form_widget(form.minplayers, {'attr': {'class': 'form-control'}}) }}
            </div>
            {{ form_label(form.maxplayers, null,{'label_attr': {'class': 'col-sm-2 control-label'}}) }}
            <div class="col-sm-2">
                {{ form_errors(form.maxplayers) }}
                {{ form_widget(form.maxplayers, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div><!--/form-group-->
        <div class="form-group">
            {{ form_label(form.bookinglink, null,{'label_attr': {'class': 'col-xs-2 control-label'}}) }}
            <div class="col-sm-10">
                {{ form_errors(form.bookinglink) }}
                {{ form_widget(form.bookinglink, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>
        <div class="form-group">
            {{ form_label(form.tags, null,{'label_attr': {'class': 'col-sm-2 control-label '}}) }}
            <div class="col-sm-10">
                {{ form_errors(form.tags) }}
                {{ form_widget(form.tags, {'attr': {'class': 'form-control '}}) }}
            </div>
        </div>
        <div class="form-group">
            {{ form_label(form.mainphoto) }}
            <div class="col-sm-10">
                {{ form_errors(form.mainphoto) }}
                {{ form_widget(form.mainphoto) }}
            </div>
        </div>
        <div class="form-group">
            {{ form_label(form.photos) }}
            <div class="col-sm-10">
                {{ form_errors(form.photos) }}
                {{ form_widget(form.photos) }}
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">Изображения</label>
            <div class="col-sm-5 ">
                <div class="panel panel-default myPanel">
                    <div class="panel-heading">Главное фото</div>
                    <div class="panel-body">
                        <form ></form>
                        <form action="{{ oneup_uploader_endpoint('gallery') }}"
                              id="DropZoneForQuestMainPhoto" class="dropzone" method="post">
                            <input type="hidden" name="typeOfDropzone" value="DropZoneForQuestMainPhoto" />
                        </form>

                    </div>
                </div>
             </div>
            <div class="col-sm-5">
                <div class="panel panel-default myPanel">
                    <div class="panel-heading">Галерея</div>
                    <div class="panel-body">
                        <form action="{{ oneup_uploader_endpoint('gallery') }}"
                              id="DropZoneForQuestGallery" class="dropzone" method="post">
                            <input type="hidden" name="typeOfDropzone" value="DropZoneForQuestGallery" />
                        </form>
                    </div>
                </div>
            </div>
        </div><!--/form-group-->
    {{ form_end(form) }}
</div>

formType.php

class formType extends AbstractType
{

    private $manager;
    protected $em;

    public function __construct(ObjectManager $manager) {
        $this->manager = $manager;
    }
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class,array(
                'label' => 'Название квеста',
                'attr' => array(
                    'placeholder'=>"Название квеста",
                        'help'  => 'Task name should be short and actionable'
                      )))
            ->add('description', TextareaType::class,array(
                'label' => 'Описание',
                'attr' => array(
                    'placeholder'=>"Описание",
                )))
            ->add('organizer',EntityType::class,array(
                'label' => 'Организатор',
                'class' => 'AppBundle:Organizer',
                'choice_label' => 'name',
                'empty_data'=> 'choose',

                'attr' => array(
                    'class'=>'cityDroplist form-control',
                    'placeholder' => "Выберите квест",
                    )))
            ->add('city',EntityType::class,array(
                'label' => 'Город',
                'class' => 'AppBundle:City',
                'placeholder' => "Выберите город",
                'choice_label' => 'name',
                'attr' => array('class'=>'cityDroplist form-control')))
            ->add('address', TextType::class,array('label' => 'Адрес',
                'attr' => array(
                    'placeholder'=>"Адрес",
                )))
            ->add('metro', TextType::class,array('label' => 'Метро',
                'attr' => array(
                    'placeholder'=>"Метро",
                )))
            ->add('phone', TextType::class,array('label' => 'Телефон',
                'attr' => array(
                    'placeholder'=>"Телефон",
                )))
            ->add('mail', TextType::class,array(
                'label' => 'Почта',
                'attr' => array(
                    'placeholder'=>"Почта",
                )))
            ->add('minplayers', TextType::class,array(
                'label' => false,
                'attr' => array(
                    'placeholder' => 'Min',
                )))
            ->add('maxplayers', TextType::class,array(
                'label' =>false,
                'attr' => array(
                    'placeholder' => 'Max',
                    )))
            ->add('minprice', TextType::class,array(
                'label' => false,
                'attr' => array(
                    'placeholder' => 'Min',
                )))
            ->add('maxprice', TextType::class,array(
                'label' => false,
                'attr' => array(
                    'placeholder' => 'Max',
                )))
            ->add('bookinglink', TextType::class,array(
                'label' => 'Ссылка на бронирование',
                'attr' => array(
                    'placeholder' => 'Ссылка на бронирование',
                )))
            ->add('photos',HiddenType::class,array(
                'label'=>false,
                'required' => false))
            ->add('mainphoto',HiddenType::class,array(
                'label'=>false,
                'required' => false,
                'attr' => array(
                )))
            ->add('tags',EntityType::class,array(
                'label' => 'Tag',
                'class' => 'AppBundle:Tag',
                'choice_label' => 'name',
                'expanded' => true,
                'multiple' => true,
                ))
            ->add('save', SubmitType::class, array('label' => 'Отправить'));


            $builder->get('photos')
                ->addModelTransformer(new IssueToNumberTransformer($this->manager));
            $builder->get('mainphoto')
                ->addModelTransformer(new formNumberToUploadedFile($this->manager));

    }

}

0 个答案:

没有答案