Php 强制不需要字段

Php 强制不需要字段,php,symfony,orm,doctrine-orm,Php,Symfony,Orm,Doctrine Orm,我已经读过了。但这没用。我添加了'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; c

我已经读过了。但这没用。我添加了'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
                    )
                )
我已确保实体中的地址可为空:

<?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;

我错过了什么?注意:我已经按照下面的步骤使动态字段添加一个或多个地址

我认为您的问题在于约束,您应该编写如下内容:

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;

    //...
}

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

'required'=>false应该可以。也许你需要清理缓存?我一直都在做。php应用程序/控制台缓存:清除和丑陋的一个:rm-rf应用程序/缓存;mkdir app/cache&&chmod 777 app/cache,无论symfony doc怎么说,这都是经常需要的