Added a localized check to the expiration date

This commit is contained in:
Franck Allimant
2014-05-02 18:23:36 +02:00
parent 3b7589ec84
commit 5e3585766d

View File

@@ -12,11 +12,13 @@
namespace Thelia\Form; namespace Thelia\Form;
use Symfony\Component\Validator\Constraints\Date; use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual; use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotEqualTo; use Symfony\Component\Validator\Constraints\NotEqualTo;
use Thelia\Coupon\ExpirationDateTransformer; use Symfony\Component\Validator\ExecutionContextInterface;
use Thelia\Core\Translation\Translator;
use Thelia\Model\Base\LangQuery;
/** /**
* Allow to build a form Coupon * Allow to build a form Coupon
@@ -91,16 +93,18 @@ class CouponCreationForm extends BaseForm
array() array()
) )
->add( ->add(
$this->formBuilder->create( 'expirationDate',
'expirationDate', 'text',
'text', array(
array( 'constraints' => array(
'constraints' => array( new NotBlank(),
new NotBlank(), new Callback(array(
//new Date() "methods" => array(
) array($this, "checkLocalizedDate"),
),
))
) )
)->addViewTransformer(new ExpirationDateTransformer()) )
) )
->add( ->add(
'isCumulative', 'isCumulative',
@@ -142,6 +146,24 @@ class CouponCreationForm extends BaseForm
); );
} }
/**
* Validate a date entered with the default Language date format.
*
* @param string $value
* @param ExecutionContextInterface $context
*/
public function checkLocalizedDate($value, ExecutionContextInterface $context)
{
$format = LangQuery::create()->findOneByByDefault(true)->getDateFormat();
if (false === \DateTime::createFromFormat($format, $value)) {
$context->addViolation(Translator::getInstance()->trans("Date '%date' is invalid, please enter a valid date using %fmt format", [
'%fmt' => $format,
'%date' => $value
]));
}
}
/** /**
* Get form name * Get form name
* *