2014-03-18 42 views
1

我實際上有一個問題。 Invetory實體的財產「數量」不應該是負數。Symfony2資產GreaterThan沒有工作

所以我嘗試在我的實體聲明中使用GreaterThan或GreaterThanOrEqual斷言。

事實上,我可以驗證負數量。 我不明白。

實體:

/* src/Clicproxy/***Bundle/Entity/Inventory.php */ 
<?php 

namespace Clicproxy\***Bundle\Entity; 

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


/** 
* Inventory 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="Clicproxy\***Bundle\Entity\InventoryRepository") 
* @UniqueEntity(fields="audit, name", message="entity.inventory.unique") 
*/ 
class Inventory 
{ 
    /* [...] */ 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="quantity", type="integer", nullable=true) 
    * @Assert\GreaterThan(value = 1) 
    */ 
    private $quantity; 

[...] 

的FormType:

控制器:

public function configAction (Request $request, $slug) 
{ 
    $em = $this->getDoctrine()->getManager(); 
    $audit = $em->getRepository('Clicproxy***Bundle:Audit')->findOneBy(array('slug' => $slug)); 

    if (!$audit instanceof Audit) { 
     throw $this->createNotFoundException('wizard.config.notfound'); 
    } 

    $audit->addInventoriesFromEquipments($em->getRepository('Clicproxy***Bundle:Equipment')->findBy(array(), array('optimized' => 'ASC', 'name'=> 'ASC'))); 

    $form = $this->createCreateConfigForm($audit); 

    $form->handleRequest($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($audit); 
     foreach ($audit->getInventories() as $inventory) { 
      $inventory->setAudit($audit); 
      $em->persist($inventory); 
     } 
     $em->flush(); 

     /* [...] */ 

     return $this->redirect($this->generateUrl('wizard_result', array('slug' => $audit->getSlug()))); 
    } 

    /* [...] */ 

    return array(
     'audit' => $audit, 
     'form' => $form->createView(), 
     'tabactive' => 2, 
    ); 
} 

有誰對我的背景下的想法?

感謝您的支持, David。

編輯: 最終我在下面寫下這段代碼,您的意見?

public function configAction (Request $request, $slug) 
{ 
    $em = $this->getDoctrine()->getManager(); 
    $audit = $em->getRepository('Clicproxy***Bundle:Audit')->findOneBy(array('slug' => $slug)); 

    if (!$audit instanceof Audit) { 
     throw $this->createNotFoundException('wizard.config.notfound'); 
    } 

    $audit->addInventoriesFromEquipments($em->getRepository('Clicproxy***Bundle:Equipment')->findBy(array(), array('optimized' => 'ASC', 'name'=> 'ASC'))); 

    $form = $this->createCreateConfigForm($audit); 

    $form->handleRequest($request); 

    if ($form->isValid()) { 
     $validator = $this->get('validator'); 
     $errors_messages = array(); 
     foreach ($audit->getInventories() as $inventory) 
     { 
      $violations = $validator->validate($inventory); 
      if (0 < $violations->count()) 
      { 
       $error_message = substr($violations, strpos($violations, ':')+2); 
       if (! in_array($error_message, $errors_messages, true)) { 
        $errors_messages[] = $error_message; 
        $this->get('session')->getFlashBag()->add('error', $error_message); 
       } 

      } 
     } 
     if (! $this->get('session')->getFlashBag()->has('error')) 
     { 
      $em = $this->getDoctrine()->getManager(); 
      $em->persist($audit); 
      foreach ($audit->getInventories() as $inventory) { 
       $inventory->setAudit($audit); 
       $em->persist($inventory); 
      } 
      $em->flush(); 

      /* [...] */ 

      return $this->redirect($this->generateUrl('wizard_result', array('slug' => $audit->getSlug()))); 
     } 
    } 


    return array(
     'audit' => $audit, 
     'form' => $form->createView(), 
     'tabactive' => 2, 
    ); 
} 

感謝您的支持。

+0

你可以顯示你用來處理表單提交的邏輯嗎? – Peter

+0

我剛剛添加了邏輯。 – David

回答

0

您不驗證您的實體,只是您的表單。

這是一個常見的錯誤,假設您撥打$form->isValid()您的Doctrine實體正在驗證,但事實並非如此。

您需要顯式調用驗證器服務,並從驗證表單中單獨處理。

這會看起來像這樣:

$validator = $this->get('validator'); 
$errors = $validator->validate($inventory); 

if (count($errors) > 0) { 
    // Handle errors here 
} 

欲瞭解更多信息,請看看validation documentation

+0

我只是添加了我剛寫的代碼,你能給我你的意見嗎? – David

+0

您應該在if語句中返回一個錯誤,以檢查是否存在違規。那和'0 <$ violation-> count()'真的很尷尬。編寫'$ violations-> count()> 0'更具可讀性。除此之外,它看起來不錯 – Peter

+0

我已經添加了'$ form-> get('XXX') - > addError(new FormError('Message'));'處理模板中的錯誤信息。 – David