From d05e28dc43f4a4daac72de19d93f611a72e0a961 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 4 Mar 2014 10:06:47 +0100 Subject: [PATCH] verify if coupon code is existing --- core/lib/Thelia/Form/CouponCode.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/core/lib/Thelia/Form/CouponCode.php b/core/lib/Thelia/Form/CouponCode.php index d9b546f1f..1319c1fb8 100644 --- a/core/lib/Thelia/Form/CouponCode.php +++ b/core/lib/Thelia/Form/CouponCode.php @@ -23,6 +23,9 @@ namespace Thelia\Form; use Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\ExecutionContextInterface; +use Thelia\Core\Translation\Translator; +use Thelia\Model\CouponQuery; /** * Class CouponCode @@ -44,11 +47,25 @@ class CouponCode extends BaseForm "required" => true, "constraints" => array( new Constraints\NotBlank(), + new Constraints\Callback(array( + "methods" => array( + array($this, + "verifyExistingCode") + ) + )) ) ) ); } + public function verifyExistingCode($value, ExecutionContextInterface $context) + { + $coupon = CouponQuery::create()->findOneByCode($value); + if (null === $coupon) { + $context->addViolation(Translator::getInstance()->trans("This coupon does not exists")); + } + } + /** * Form name *