我已经读过这个了。但这无济于事。我已经添加了'required' => false
但没有任何反应。
<?php
namespace BNBundleXxBundleFormType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolver;
use FOSUserBundleFormTypeRegistrationFormType 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 BNBundleXxBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Adresse
*
* @ORMTable(name="adresse")
* @ORMEntity
* @ORMHasLifecycleCallbacks()
*/
class Adresse
{
/**
* @var integer
*
* @ORMColumn(name="id", type="bigint", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
//...
// some other code...
//...
/**
* @var string
*
* @ORMColumn(name="adresse1", type="string", length=250, nullable=true)
*/
private $adresse1;
/**
* @var string
*
* @ORMColumn(name="adresse2", type="string", length=250, nullable=true)
*/
private $adresse2;
我错过了什么?注意:我按照这个官方导师制作了动态字段(添加一个或多个地址)
我认为你的问题是约束,你应该写这样的东西:
namespace BNBundleXxBundleEntity;
use SymfonyComponentValidatorConstraints as Assert;
use DoctrineORMMapping as ORM;
/**
* Adresse
*
* @ORMTable(name="adresse")
* @ORMEntity
* @ORMHasLifecycleCallbacks()
*/
class Adresse
{
//...
/**
* @var string
*
* @ORMColumn(name="adresse1", type="string", length=250, nullable=true)
* @AssertNull()
*/
private $adresse1;
//...
}
如果我错了,请告诉我。祝你好运!