1

我正在尝试解决我的问题,但经过数小时的搜索,它不起作用:(

所以我有一堂课:

<?php

namespace Application\HappyBundle\Entity;

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

/**
 * Bar
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Application\HappyBundle\Entity\BarRepository")
 */
class Bar
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * @Assert\NotBlank()
     * @ORM\Column(name="name", type="string", length=45)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="address", type="string", length=255)
     */
    private $address;

    /**
     * @var string
     *
     * @ORM\Column(name="addressComplete", type="string", length=255)
     */
    private $addressComplete;

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

    /**
     * @var string
     *
     * @ORM\Column(name="town", type="string", length=45)
     */
    private $town;

    /**
     * @var string
     *
     * @ORM\Column(name="country", type="string", length=45)
     */
    private $country;

    /**
     * @var string
     *
     * @ORM\Column(name="latitude", type="string", length=20)
     */
    private $latitude;

    /**
     * @var string
     *
     * @ORM\Column(name="longitude", type="string", length=20)
     */
    private $longitude;

    /**
     * @var array
     *
     * @ORM\Column(name="validate", type="boolean", nullable=false)
     */
    private $validate;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="last_update", type="datetime", nullable=true)
     */
    private $lastUpdate;

    /**
    * @var ArrayCollection of AssocBarDay
    * @Assert\Valid
    * @ORM\OneToMany(targetEntity="Application\HappyBundle\Entity\AssocBarDay", mappedBy="bar" ,  cascade={"persist" , "remove"})
     */
    private $day;

    /**
     * @ORM\OneToMany(targetEntity="Application\HappyBundle\Entity\AssocBarDrink", mappedBy="drink", cascade={"persist"})
     */
    private $drink;

    /**
     * @ORM\OneToMany(targetEntity="Application\HappyBundle\Entity\Bar", mappedBy="barVersionned", cascade={"persist"})
     */
    private $barHaveVersions;

    /**
     * @ORM\ManyToOne(targetEntity="Application\HappyBundle\Entity\Bar", inversedBy="barHaveVersions" , cascade={"persist"})
     */
    private $barVersionned;


    /**
     * Constructor
     */
    public function __construct()
    {
        $this->day          = new \Doctrine\Common\Collections\ArrayCollection();
        $this->drink        = new \Doctrine\Common\Collections\ArrayCollection();
        $this->validate     = false;
        $this->lastUpdate   = new \DateTime('now');
    }

    public function __toString(){
        return $this->name;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Bar
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set address
     *
     * @param string $address
     * @return Bar
     */
    public function setAddress($address)
    {
        $this->address = $address;

        return $this;
    }

    /**
     * Get address
     *
     * @return string
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * Set postalCode
     *
     * @param string $postalCode
     * @return Bar
     */
    public function setPostalCode($postalCode)
    {
        $this->postalCode = $postalCode;

        return $this;
    }

    /**
     * Get postalCode
     *
     * @return string
     */
    public function getPostalCode()
    {
        return $this->postalCode;
    }

    /**
     * Set town
     *
     * @param string $town
     * @return Bar
     */
    public function setTown($town)
    {
        $this->town = $town;

        return $this;
    }

    /**
     * Get town
     *
     * @return string
     */
    public function getTown()
    {
        return $this->town;
    }

    /**
     * Set country
     *
     * @param string $country
     * @return Bar
     */
    public function setCountry($country)
    {
        $this->country = $country;

        return $this;
    }

    /**
     * Get country
     *
     * @return string
     */
    public function getCountry()
    {
        return $this->country;
    }

    /**
     * Set latitude
     *
     * @param string $latitude
     * @return Bar
     */
    public function setLatitude($latitude)
    {
        $this->latitude = $latitude;

        return $this;
    }

    /**
     * Get latitude
     *
     * @return string
     */
    public function getLatitude()
    {
        return $this->latitude;
    }

    /**
     * Set longitude
     *
     * @param string $longitude
     * @return Bar
     */
    public function setLongitude($longitude)
    {
        $this->longitude = $longitude;

        return $this;
    }

    /**
     * Get longitude
     *
     * @return string
     */
    public function getLongitude()
    {
        return $this->longitude;
    }


    /**
     * Add drink
     *
     * @param \Application\HappyBundle\Entity\AssocBarDrink $drink
     * @return Bar
     */
    public function addDrink(\Application\HappyBundle\Entity\AssocBarDrink $drink)
    {
        $this->drink[] = $drink;

        return $this;
    }

    /**
     * Remove drink
     *
     * @param \Application\HappyBundle\Entity\AssocBarDrink $drink
     */
    public function removeDrink(\Application\HappyBundle\Entity\AssocBarDrink $drink)
    {
        $this->drink->removeElement($drink);
    }

    /**
     * Get drink
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getDrink()
    {
        return $this->drink;
    }


    /**
     * Add day
     *
     * @param \Application\HappyBundle\Entity\AssocBarDay $day
     * @return Bar
     */
    public function addDay(\Application\HappyBundle\Entity\AssocBarDay $day)
    {
        $this->day[] = $day;

        return $this;
    }

    /**
     * Remove day
     *
     * @param \Application\HappyBundle\Entity\AssocBarDay $day
     */
    public function removeDay(\Application\HappyBundle\Entity\AssocBarDay $day)
    {
        $this->day->removeElement($day);
    }

    /**
     * Get day
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getDay()
    {
        return $this->day;
    }

    /**
     * Set lastUpdate
     *
     * @param \DateTime $lastUpdate
     * @return Bar
     */
    public function setLastUpdate($lastUpdate)
    {
        $this->lastUpdate = $lastUpdate;

        return $this;
    }

    /**
     * Get lastUpdate
     *
     * @return \DateTime
     */
    public function getLastUpdate()
    {
        return $this->lastUpdate;
    }

    /**
     * Set validate
     *
     * @param boolean $validate
     * @return Bar
     */
    public function setValidate($validate)
    {
        $this->validate = $validate;

        return $this;
    }

    /**
     * Get validate
     *
     * @return boolean
     */
    public function getValidate()
    {
        return $this->validate;
    }

    /**
     * Add barHaveVersions
     *
     * @param \Application\HappyBundle\Entity\Bar $barHaveVersions
     * @return Bar
     */
    public function addBarHaveVersion(\Application\HappyBundle\Entity\Bar $barHaveVersions)
    {
        $this->barHaveVersions[] = $barHaveVersions;

        return $this;
    }

    /**
     * Remove barHaveVersions
     *
     * @param \Application\HappyBundle\Entity\Bar $barHaveVersions
     */
    public function removeBarHaveVersion(\Application\HappyBundle\Entity\Bar $barHaveVersions)
    {
        $this->barHaveVersions->removeElement($barHaveVersions);
    }

    /**
     * Get barHaveVersions
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getBarHaveVersions()
    {
        return $this->barHaveVersions;
    }

    /**
     * Set barVersionned
     *
     * @param \Application\HappyBundle\Entity\Bar $barVersionned
     * @return Bar
     */
    public function setBarVersionned(\Application\HappyBundle\Entity\Bar $barVersionned = null)
    {
        $this->barVersionned = $barVersionned;

        return $this;
    }

    /**
     * Get barVersionned
     *
     * @return \Application\HappyBundle\Entity\Bar
     */
    public function getBarVersionned()
    {
        return $this->barVersionned;
    }

    /**
     * Set addressComplete
     *
     * @param string $addressComplete
     * @return Bar
     */
    public function setAddressComplete($addressComplete)
    {
        $this->addressComplete = $addressComplete;

        return $this;
    }

    /**
     * Get addressComplete
     *
     * @return string 
     */
    public function getAddressComplete()
    {
        return $this->addressComplete;
    }
}

和 OneToMany 这个类:

<?php

namespace Application\HappyBundle\Entity;

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

/**
 * AssocBarDayHappyHour
 * @ORM\Table(name="assoc_bar_day")
 * @ORM\Entity(repositoryClass="Application\HappyBundle\Entity\AssocBarDayRepository")
 */
class AssocBarDay
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Application\HappyBundle\Entity\Bar", inversedBy="day" , cascade={"persist"})
     */
    private $bar;

    /**
     * @var array
     * @ORM\Column(name="day", type="array", nullable=true)
     */
    private $day;

    /**
     * @var \DateTime
     * @Assert\Time()
     * @ORM\Column(name="time_start_happy_hour", type="time", nullable=true)
     */
    private $timeStartHappyHour;

    /**
     * @var \DateTime
     * @Assert\Time()
     * @ORM\Column(name="time_end_happy_hour", type="time", nullable=true)
     */
    private $timeEndHappyHour;

    /**
     * @var \DateTime
     * @Assert\Time()
     * @ORM\Column(name="time_bar_open", type="time", nullable=true)
     */
    private $timeBarOpen;

    /**
     * @var \DateTime
     * @Assert\Time()
     * @ORM\Column(name="time_bar_close", type="time" , nullable=true)
     */
    private $timeBarClose;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set timeStartHappyHour
     *
     * @param \DateTime $timeStartHappyHour
     * @return AssocBarDay
     */
    public function setTimeStartHappyHour($timeStartHappyHour)
    {
        $this->timeStartHappyHour = $timeStartHappyHour;

        return $this;
    }

    /**
     * Get timeStartHappyHour
     *
     * @return \DateTime
     */
    public function getTimeStartHappyHour()
    {
        return $this->timeStartHappyHour;
    }

    /**
     * Set timeEndHappyHour
     *
     * @param \DateTime $timeEndHappyHour
     * @return AssocBarDay
     */
    public function setTimeEndHappyHour($timeEndHappyHour)
    {
        $this->timeEndHappyHour = $timeEndHappyHour;

        return $this;
    }

    /**
     * Get timeEndHappyHour
     *
     * @return \DateTime
     */
    public function getTimeEndHappyHour()
    {
        return $this->timeEndHappyHour;
    }

    /**
     * Set timeBarOpen
     *
     * @param \DateTime $timeBarOpen
     * @return AssocBarDay
     */
    public function setTimeBarOpen($timeBarOpen)
    {
        $this->timeBarOpen = $timeBarOpen;

        return $this;
    }

    /**
     * Get timeBarOpen
     *
     * @return \DateTime
     */
    public function getTimeBarOpen()
    {
        return $this->timeBarOpen;
    }

    /**
     * Set timeBarClose
     *
     * @param \DateTime $timeBarClose
     * @return AssocBarDay
     */
    public function setTimeBarClose($timeBarClose)
    {
        $this->timeBarClose = $timeBarClose;

        return $this;
    }

    /**
     * Get timeBarClose
     *
     * @return \DateTime
     */
    public function getTimeBarClose()
    {
        return $this->timeBarClose;
    }

    /**
     * Set bar
     *
     * @param \Application\HappyBundle\Entity\Bar $bar
     * @return AssocBarDay
     */
    public function setBar(\Application\HappyBundle\Entity\Bar $bar = null)
    {
        $this->bar = $bar;

        return $this;
    }

    /**
     * Get bar
     *
     * @return \Application\HappyBundle\Entity\Bar
     */
    public function getBar()
    {
        return $this->bar;
    }


    /**
     * Set day
     *
     * @param array $day
     * @return AssocBarDay
     */
    public function setDay($day)
    {
        $this->day = $day;

        return $this;
    }

    /**
     * Get day
     *
     * @return array
     */
    public function getDay()
    {
        return $this->day;
    }
}

因此,当我发布表单时,天数集合的断言在时间为空时不会阻塞?

我正确输入了:

@Assert\Valid

在一天收集和

@Assert\Time()

AssocBarDay 的每个属性

:( 有人我有想法吗?

谢谢你的帮助。

4

1 回答 1

1

原因是几乎所有的验证约束都不会为空/空值返回无效。

看看 TimeValidator 的validate 方法...

if (null === $value || '' === $value || $value instanceof \DateTime) {
     return;
}

因此,您需要另外添加@Assert\NotBlank约束,以使空值生成该属性的验证错误。

非空白

验证一个值不为空,定义为不等于空字符串,也不等于 null。

于 2013-10-26T11:11:05.257 回答