TextField上需要Symfony渲染为true

时间:2017-04-08 12:53:52

标签: symfony

我不明白为什么此子表单的呈现不会在我的TextType required上呈现firstName标记;

我的表单基于Order实体
OrderFormType的CollectionType为Tent,基于TentFormType
TentFormType的CollectionType为Camper,基于CamperFormType

所以订单>帐篷>坎珀

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
//...

class CamperFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('firstName', TextType::class, [
            'required' => true, //Should even not been usefull since SF2.8
            'label'    => 'First name',
            'attr'     => [
                'placeholder' => 'First name'
            ],
        ]);

        //...
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class'      => 'AppBundle\Entity\Camper',
            'csrf_protection' => true,
            'error_bubbling'  => true,
            'csrf_field_name' => '_token',
            //...
        ]);
    }
}

使用form_widget简单地渲染字段:

{{ form_widget(form.firstName) }}
{{ form_widget(form.lastName) }}

但是没有添加required字段:

<input id="app_order_form_type_tents_0_campers_0_firstName" name="app_order_form_type[tents][0][campers][0][firstName]" placeholder="First name" class="form-control" type="text">
<input id="app_order_form_type_tents_0_campers_0_lastName" name="app_order_form_type[tents][0][campers][0][lastName]" placeholder="Last name" class="form-control" type="text">

我能做到

{{ form_widget(form.firstName, {'attr': {'required': 'required'}}) }}
{{ form_widget(form.lastName, {'attr': {'required': 'required'}}) }}

但我的FormType不应该被要求......

有谁知道为什么?

- 编辑 -

我的Camper实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Camper.
 *
 * @ORM\Table(name="camper")
 * @ORM\Entity()
 */
class Camper
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * @Assert\NotBlank()
     *
     * @ORM\Column(name="firstName", type="string", length=255, nullable=false)
     */
    private $firstName;

    // ...

}

1 个答案:

答案 0 :(得分:0)

对不起,我不能用评论,所以我在这里提出建议...... 试试:

log