Example #1
0
 /**
  * @param \Sonata\AdminBundle\Validator\ErrorElement $errorElement
  * @param $object
  * @return void
  */
 public function validate(ErrorElement $errorElement, $object)
 {
     if (!$object->getUrl()) {
         $this->cmsManager->getPageManager()->fixUrl($object);
     }
     $page = $this->cmsManager->getPageManager()->getPageByUrl($object->getUrl());
     if (!$page) {
         $page = $this->cmsManager->getPageManager()->getPageByUrl(substr($object->getUrl(), -1) == '/' ? substr($object->getUrl(), 0, -1) : $object->getUrl() . '/');
     }
     if ($page && $page->getId() != $object->getId()) {
         $errorElement->addViolation($this->trans('error.uniq_url', array('%url%' => $object->getUrl())));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validateFormBasketElement(ErrorElement $errorElement, BasketElementInterface $basketElement, BasketInterface $basket)
 {
     // the item is flagged as deleted, no need to validate the item
     if ($basketElement->getDelete() || 0 === $basketElement->getQuantity()) {
         return;
     }
     // refresh the product from the database
     $product = $basketElement->getProduct();
     // check if the product is still in database
     if (!$product) {
         $errorElement->addViolation('product_not_available');
         return;
     }
     // check if the product is still enabled
     if (!$basketElement->getProduct()->getEnabled()) {
         $errorElement->addViolation('product_not_enabled');
         return;
     }
     // check if the quantity is numeric
     if (!is_numeric($basketElement->getQuantity())) {
         $errorElement->with('quantity')->addViolation('product_quantity_not_numeric')->end();
         return;
     }
     $errorElement->with('quantity')->assertRange(array('min' => 1, 'max' => $this->getStockAvailable($basketElement->getProduct()), 'minMessage' => 'basket_quantity_limit_min', 'maxMessage' => 'basket_quantity_limit_max'))->end();
 }