2013-05-31 28 views
7

林在我的CRUD的應用程序具有與該的addAction一個問題。在控制器上,邏輯不會通過$ form-> isValid()驗證,但表單不顯示任何錯誤消息。

我這個(感謝山姆)的嘗試:

foreach($form->get('product')->getElements() as $el) 
{ 
    echo $el->getName()." = ".$el->getValue()." > ".$el->getMessages()." <br/>"; 
} 

即只顯示名稱和字段的值,而不是錯誤消息。

我試着讓形式完全空白,它火「所需的價值,不能爲空」的錯誤消息,但後來,我填寫每場一個接一個,直到我沒有得到更多的錯誤消息,但該表格仍然無效。

我的形式所具有的產品字段集作爲基礎字段集和一個提交按鈕。 Inside Product Fieldset中有一個ID字段,一個名稱字段,一個價格字段和一個品牌字段集。在我的品牌字段集中,我有一個ID字段。就像這樣:

ProductForm:

class ProductForm extends Form 
{ 

    public function init() 
    { 
     // we want to ignore the name passed 
     parent::__construct('product'); 
     $this->setName('product'); 
     $this->setAttribute('method', 'post'); 

     $this->add(array(
       'name' => 'product', 
       'type' => 'Administrador\Form\ProductFieldset', 
       'options' => array(
         'use_as_base_fieldset' => true 
       ), 
     )); 
     $this->add(array(
       'name' => 'submit', 
       'type' => 'Submit', 
       'attributes' => array(
         'value' => 'Add', 
         'id' => 'submitbutton', 
       ), 
     )); 
    } 
} 

ProductFieldset:

class ProductFieldset extends Fieldset implements ServiceLocatorAwareInterface 
{ 

    protected $serviceLocator; 

    function __construct($name = null) 
    { 
     parent::__construct('product_fieldset'); 

     $this->setHydrator(new ArraySerializableHydrator()); 
     $this->setObject(new Product()); 
    } 

    public function init() 
    { 

     $this->add(array(
       'name' => 'id', 
       'type' => 'Hidden', 
     )); 
     $this->add(array(
       'name' => 'name', 
       'type' => 'Text', 
       'options' => array(
         'label' => 'Name', 
       ), 
     )); 
     $this->add(array(
       'name' => 'price', 
       'type' => 'Text', 
       'options' => array(
         'label' => 'Price', 
       ), 
     )); 
     $this->add(array(
       'name' => 'brand', 
       'type' => 'BrandFieldset', 
     )); 
    } 


    public function setServiceLocator(ServiceLocatorInterface $sl) 
    { 
     $this->serviceLocator = $sl; 
    } 

    public function getServiceLocator() 
    { 
     return $this->serviceLocator; 
    } 
} 

BrandFieldset:

class BrandFieldset extends Fieldset 
{ 
    function __construct(BrandTable $brandTable) 
    { 
     parent::__construct('brand_fieldset'); 

     //$this->setHydrator(new ClassMethodsHydrator(false))->setObject(new Brand()); 

     $this->setHydrator(new ArraySerializableHydrator()); 
     $this->setObject(new Brand()); 

     $brandSelectOptionsArray = $brandTable->populateSelectBrand(); 
     $this->add(array(
       'name' => 'id', 
       'type' => 'Select', 
       'options' => array(
         'label' => 'Brand', 
         'empty_option' => 'Please select a brand', 
         'value_options' => $brandSelectOptionsArray, 
       ), 
     )); 
    } 
} 

這是我的addAction新的Form聲明:

$formManager = $this->serviceLocator->get('FormElementManager'); 
$form = $formManager->get('Administrador\Form\ProductForm'); 

在我的模型「產品」我有「身份證」字段,需要過濾器「名稱」字段inputFilters,所需的過濾器。而對於品牌領域,我創建其他輸入過濾器,並將其添加到主輸入過濾:

$brandFilter->add($factory->createInput(array(
'name'  => 'id', 
'required' => true, 
'filters' => array(
    array('name' => 'Int'), 
), 
))); 
$inputFilter->add($brandFilter, 'brand'); 

怪異的行爲是我的editAction工作正常,並具有相同的邏輯。

是否有任何形式的呼應從形式上的東西,可以幫助我理解爲什麼形式是無效的內部錯誤消息。

EDIT 2013年6月1日

這裏是我的全部控制器:

class ProductController extends AbstractActionController 
{ 

    protected $productTable; 
    protected $brandTable; 

    public function indexAction() 
    { 
     return new ViewModel(array(
      'products' => $this->getProductTable()->fetchAll(), 
     )); 
    } 

    public function addAction() 
    { 
     $formManager = $this->serviceLocator->get('FormElementManager'); 
     $form = $formManager->get('Administrador\Form\ProductForm'); 
     $form->get('submit')->setValue('Add'); 

     $request = $this->getRequest(); 
     if ($request->isPost()) { 
      $product = new Product(); 
      $product->brand = new Brand(); 
      $form->setInputFilter($product->getInputFilter()); 
      $form->setData($request->getPost()); 

      if ($form->isValid()) { 
       $product->exchangeArray($form->getData()); 
       $this->getProductTable()->saveProduct($product); 

       // Redirect to list of products 
       return $this->redirect()->toRoute('product'); 
      } 
     } 
     return new ViewModel(array(
      'form' => $form, 
     )); 
    } 

    public function editAction() 
    { 
     $id = (int) $this->params()->fromRoute('id', 0); 
     if (!$id) { 
      return $this->redirect()->toRoute('product', array(
        'action' => 'add' 
      )); 
     } 

     // Get the Product with the specified id. An exception is thrown 
     // if it cannot be found, in which case go to the index page. 
     try { 
      $product = $this->getProductTable()->getProduct($id); 
     } 
     catch (\Exception $ex) { 
      return $this->redirect()->toRoute('product', array(
        'action' => 'index' 
      )); 
     } 
     $formManager = $this->serviceLocator->get('FormElementManager'); 
     $form = $formManager->get('Administrador\Form\ProductForm'); 
     $brand = $this->getBrandTable()->getBrand($product->brand); 
     $product->brand = $brand; 
     $form->bind($product); 
     $form->get('submit')->setAttribute('value', 'Edit'); 

     $request = $this->getRequest(); 
     if ($request->isPost()) { 
      $form->setInputFilter($product->getInputFilter()); 
      $form->setData($request->getPost()); 

      if ($form->isValid()) { 
       $this->getProductTable()->saveProduct($form->getData()); 

       // Redirect to list of products 
       return $this->redirect()->toRoute('product'); 
      } 
     } 

     return array(
       'id' => $id, 
       'form' => $form, 
     ); 
    } 

    public function deleteAction() 
    { 
     $id = (int) $this->params()->fromRoute('id', 0); 
     if (!$id) { 
      return $this->redirect()->toRoute('product'); 
     } 

     $request = $this->getRequest(); 
     if ($request->isPost()) { 
      $del = $request->getPost('del', 'No'); 

      if ($del == 'Yes') { 
       $id = (int) $request->getPost('id'); 
       $this->getProductTable()->deleteProduct($id); 
      } 

      // Redirect to list of products 
      return $this->redirect()->toRoute('product'); 
     } 

     return array(
       'id' => $id, 
       'product' => $this->getProductTable()->getProduct($id) 
     ); 
    } 


    public function getProductTable() 
    { 
     if (!$this->productTable) { 
      $sm = $this->getServiceLocator(); 
      $this->productTable = $sm->get('Administrador\Model\ProductTable'); 
     } 
     return $this->productTable; 
    } 

    public function getBrandTable() 
    { 
     if (!$this->brandTable) { 
      $sm = $this->getServiceLocator(); 
      $this->brandTable = $sm->get('Administrador\Model\BrandTable'); 
     } 
     return $this->brandTable; 
    } 
} 
+0

請提供完整的控制器方法。 – netiul

+0

好的,我會提供控制器的添加和編輯操作。 –

+0

有什麼想法?任何人? –

回答

0

好了,我得到了答案:d

這是的addAction應該如何:

public function addAction() 
{ 
    $formManager = $this->serviceLocator->get('FormElementManager'); 
    $form = $formManager->get('Administrador\Form\ProductForm'); 
    $form->get('submit')->setValue('Add'); 

    $product = new Product(); 
    $product->brand = new Brand(); 

    $form->bind($product); // I need to bind the product to the form to pass the isValid() validation 

    $request = $this->getRequest(); 
    if ($request->isPost()) { 
     $form->setInputFilter($product->getInputFilter()); 
     $form->setData($request->getPost()); 

     if ($form->isValid()) { 
      $product = $form->getData(); 
      $this->getProductTable()->saveProduct($product); 

      // Redirect to list of products 
      return $this->redirect()->toRoute('product'); 
     } 
    } 
    return new ViewModel(array(
     'form' => $form, 
    )); 
} 

顯然我需要綁定和空pr oduct對象的形式能夠通過isValid()驗證。之後,我從$ form-> getData()中檢索產品對象。

0

你也可以這樣做:$form->setBindOnValidate(false);

0

我的情況是我通過錯誤的輸入濾波器。 isValid返回false,但$form->getMessages()爲空。形式OrderForm有以下幾點:

$form->setInputFilter(new \Application\Form\UserInputFilter($er)); 

當我改變UserInputFilterOrderInputFilter它的工作原理。

相關問題