强制不要求字段

时间:2014-01-15 13:38:58

标签: php symfony orm doctrine-orm

我已经阅读了this。但这没有用。我添加了'required' => false但没有任何反应。

<?php

namespace BN\Bundle\XxBundle\Form\Type;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;

    class PersonStudentRegistrationType extends BaseType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            parent::buildForm($builder, $options);
            $builder
                ->add(
                ...
                ...
                )
                ->add(
                    'adresses',
                    'collection',
                    array(
                        'type' => new AdresseType(),
                        'allow_add' => true,
                        'by_reference' => false,
                        'required' => false
                    )
                )

我确保实体中的Adresse可以为空:

<?php

namespace BN\Bundle\XxBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Adresse
 *
 * @ORM\Table(name="adresse")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 */
class Adresse
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;
    //...
    // some other code...
    //...


    /**
     * @var string
     *
     * @ORM\Column(name="adresse1", type="string", length=250, nullable=true)
     */
    private $adresse1;

    /**
     * @var string
     *
     * @ORM\Column(name="adresse2", type="string", length=250, nullable=true)
     */
    private $adresse2;

我错过了什么?注意:我已按照this official tutoriel制作动态字段(添加一个或多个地址)

1 个答案:

答案 0 :(得分:0)

我认为你的问题是约束,你应该写一些类似的东西:

namespace BN\Bundle\XxBundle\Entity;

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

/**
 * Adresse
 *
 * @ORM\Table(name="adresse")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 */
class Adresse
{
    //...

    /**
     * @var string
     *
     * @ORM\Column(name="adresse1", type="string", length=250, nullable=true)
     * @Assert\Null()
     */
    private $adresse1;

    //...
}

如果我错了,请告诉我。祝你好运!