1

次のようなドキュメントで定義されているように、コールバック検証制約を使用しています:-

/**
 * GD\AdminBundle\Entity\Offer
 *
 * @ORM\Table(name="offers")
 * @ORM\Entity(repositoryClass="GD\AdminBundle\Repository\OfferRepository")
 * @Assert\Callback(methods={"isDateValid"})
 */
class Offer
{

...

public function isDateValid(ExecutionContext $context)
    {
        // This code block gets executed but $this->getEndDate() is NULL
        if( $this->getEndDate()->getTimestamp() < $this->getStartDate()->getTimestamp() ){
            $context->addViolation('End Date cannot be less than Start Date.', array(), null);
        }
    }

ただし、var_dump を実行してテストすると$this->getEndDate()NULL

SonataAdminBundle を使用して、管理者から新しいオファー インスタンスを作成しています。 ここで何が間違っていますか?

4

2 に答える 2

0

正しい名前空間を使用しているようです。

use Symfony\Component\Validator\ExecutionContext;
use Symfony\Component\Validator\Constraints as Assert;

この二つはありますか?

于 2012-05-07T06:57:11.550 に答える