Collection Constrain

14 views
Skip to first unread message

Bodo Kaiser

unread,
Mar 16, 2012, 2:15:05 AM3/16/12
to symfony...@googlegroups.com
Hallo,

Ich habe folgendes Problem:
Ich habe eine große, OneToMany Entität, welche ich mithilfe von Form Collections befüllen will.

Da ich dazu auch eine Validierung brauche, habe ich meine Entität mit dem Collection Constrain bestückt.
Leider funktioniert die Validierung ganz und gar nicht.

Je nach dem, ob ich allowExtraFields, allowMissingFields auf true setze oder nicht, speichert Symfony2 alle Eingaben ohne Validierung oder speichert gar nichts, mit dem Fehler, dass ein unbekanntes Feld dabei ist, dabei habe ich aber alle Felder definiert.

Hier die Entität:
<?php

namespace PUS\AppBundle\Entity;

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

/**
 * @ORM\Entity(repositoryClass="PUS\AppBundle\Repository\CustomerRepository")
 * @ORM\Table(name="pus_customers")
 */
class Customer
{
    /**
     * @ORM\Id()
     * @ORM\Column(name="customer_id", type="integer")
     * @ORM\GeneratedValue()
     */
    private $customerId;

    /**
     * @Assert\Collection(
     *     fields = {
     *          "particular_id" = {
     *              @Assert\MaxLength(limit = 3)
     *          },
     *         "gender" = {
     *              @Assert\Choice(choices = {"m", "f"}, message = "Choose a valid gender")
     *          },
     *          "title" = {
     *              @Assert\Choice(choices = {"Prof Dr.", "Dr.", null}, message = "Falscher Titel")
     *          },
     *         "first_name" = {
     *             @Assert\NotBlank(),
     *             @Assert\MaxLength(limit = 30, message = "Ihr Vorname ist zu lang"),
     *             @Assert\MinLength(limit = 3, message = "Ihr Vorname ist zu kurz")
     *         },
     *          "last_name" = {
     *              @Assert\NotBlank(),
     *              @Assert\MaxLength(limit = 30, message = "Ihr Nachname ist zu lang"),
     *              @Assert\MinLength(limit = 3, message = "Ihr Nachname ist zu kurz")
     *          },
     *          "birth" = {
     *              @Assert\NotBlank(),
     *              @Assert\Date(message = "Etwas stimmt mit dem Geburtsdatum nicht")
     *          }
     *     }
     * )
     *
     * @ORM\ManyToMany(targetEntity="CustomerParticular")
     * @ORM\JoinTable(name="pus_customers_rel_customers_particulars",
     *              joinColumns={@ORM\JoinColumn(name="customer_id", referencedColumnName="customer_id")},
     *              inverseJoinColumns={@ORM\JoinColumn(name="particular_id", referencedColumnName="particular_id", unique=true)}
     *              )
     */
    private $particulars;

    /**
     * @Assert\Collection(
     *      fields = {
     *          "street" = {
     *              @Assert\NotBlank(),
     *              @Assert\MaxLength(limit = 40, message = "Ihre Stra\xc3\x9fe ist zu lang"),
     *              @Assert\MinLength(limit = 8, message = "Ihr Stra\xc3\x9fe ist zu kurz")
     *          },
     *          "streetNr" = {
     *              @Assert\NotBlank(),
     *              @Assert\MaxLength(limit = 4, message = "Ihre Stra\xc3\x9fennummer ist zu lang"),
     *              @Assert\MinLength(limit = 1, message = "Ihre Stra\xc3\x9fennummer ist zu kurz")
     *          },
     *          "zipCode" = {
     *              @Assert\NotBlank(),
     *              @Assert\MaxLength(limit = 5, message = "Ihre Postleitzahl ist zu lang"),
     *              @Assert\MinLength(limit = 5, message = "Ihre Postleitzahl ist zu kurz")
     *          },
     *          "city" = {
     *              @Assert\NotBlank()
     *          },
     *          "country" = {
     *              @Assert\NotBlank()
     *          }
     *     },
     *      allowMissingFields = true,
     *      allowExtraFields = true
     * )
     *
     * @ORM\ManyToMany(targetEntity="CustomerAddress")
     * @ORM\JoinTable(name="pus_customers_rel_customers_addresses",
     *              joinColumns={@ORM\JoinColumn(name="customer_id", referencedColumnName="customer_id")},
     *             inverseJoinColumns={@ORM\JoinColumn(name="address_id", referencedColumnName="address_id", unique=true)}
     *              )
     */
    private $addresses;
 /**
     * @Assert\Collection(
     *      fields = {
     *          "phoneNumber" = {
     *              @Assert\NotBlank(),
     *              @Assert\MaxLength(limit = 30, message = "Ihre Telefonnummer ist zu lang"),
     *              @Assert\MinLength(limit = 6, message = "Ihr Telefonnummer ist zu kurz")
     *          },
     *          "phoneType" = {
     *              @Assert\NotBlank(),
     *              @Assert\Choice(choices = {"Telefon Privat", "Telefon Gesch\xc3\xa4ftlich", "Mobile Privat", "Mobile Gesch\xc3\xa4ftlich", "Fax Privat"
, "Fax Gesch\xc3\xa4ftlich"}, message = "W\xc3\xa4hlen Sie einen Telefontyp")
     *          }
     *     },
     *      allowMissingFields = true,
     *      allowExtraFields = true
     * )
     *
     * @ORM\ManyToMany(targetEntity="CustomerPhone")
     * @ORM\JoinTable(name="pus_customers_rel_customers_phones",
     *              joinColumns={@ORM\JoinColumn(name="customer_id", referencedColumnName="customer_id")},
     *              inverseJoinColumns={@ORM\JoinColumn(name="phone_id", referencedColumnName="phone_id")}
     *              )
     */
    private $phones;

    /**
     * @Assert\Collection(
     *      fields = {
     *          "emailAddress" = {
     *              @Assert\NotBlank(),
     *              @Assert\Email(message = "Ihre Email-Addresse stimmt nicht")
     *          },
     *          "emailTypeType" = {
     *              @Assert\NotBlank(),
     *              @Assert\Choice(choices = {"Privat", "Gesch\xc3\xa4ftlich"}, message = "W\xc3\xa4hlen Sie einen Telefontyp")
     *          }
     *     },
     *      allowMissingFields = true,
     *      allowExtraFields = true
     * )
     *
     * @ORM\ManyToMany(targetEntity="CustomerEmail")
     * @ORM\JoinTable(name="pus_customers_rel_customers_emails",
     *              joinColumns={@ORM\JoinColumn(name="customer_id", referencedColumnName="customer_id")},
     *              inverseJoinColumns={@ORM\JoinColumn(name="email_id", referencedColumnName="email_id")}
     *              )
     */
    private $emails;

    public function __construct() {
        $this->particulars = new ArrayCollection();
        $this->addresses = new ArrayCollection();
        $this->phones = new ArrayCollection();
        $this->emails = new ArrayCollection();
    }

// getters and setters

Grüße,
B. Kaiser
Reply all
Reply to author
Forward
0 new messages