Reputation: 688
I have a similar problem to this: Replace an object with Null value Using Form Builder in Symfony2
I have an entity called Classification that has a ManyToOne Relationship to another entity called TypeClassification (one Classification belong to one type classification, or to none typeclassfication).
Classification.php:
/**
* @ORM\ManyToOne(targetEntity="TypeClassification", inversedBy="classifications", fetch="EAGER")
*/
private $typeclassification;
TypeClassification.php:
/**
* @ORM\OneToMany(targetEntity="Classification", mappedBy="typeclassification")
*/
private $classifications;
The form for Classification generated by default by symfony works, here is the code:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('namejapanese')
->add('ldapid')
->add('typeclassification','entity', array('class' => 'ACMEMyBundle:TypeClassification','required' => false));
}
My problem relies in the entity choice field. I would like to allow Null value, and to set the relationship to Null.
As I said it works so far, but as soon as I add validation constraints to the Entity Classification, even if they are not related to the relationship at all:
validation.yml:
ACME\MyBundle\Entity\Classification:
#If I commend this constraints, everything works properly.
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: { fields: name, message: unique.name }
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: { fields: ldapid, message: unique.ldapid }
#For this properties doesn't matter if they are here or not
properties:
name:
- NotBlank:
message: not.blank
namejapanese:
- NotBlank:
message: not.blank
ldapid:
- NotBlank:
message: not.blank
Then I am unable to set the relationship back to Null. but If I comment the constraints part, then I can set it back to Null.
Why does that happen? And How can I make it to have both this validation and the ability to set the relationship to Null?
Upvotes: 0
Views: 242