2013-05-31 6 views
7

Im avendo un problema con l'addAction nella mia applicazione CRUD. Sul controller la logica non supera la verifica $ form-> isValid() ma il modulo non mostra alcun messaggio di errore.

Ho provato con questo (Grazie Sam):

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

che mostrano solo il nome e il valore del campo, ma non il messaggio di errore.

Ho provato a lasciare il modulo completamente vuoto e ha attivato il messaggio di errore "Il valore è obbligatorio e non può essere vuoto", ma poi riempio ciascun campo uno alla volta fino a quando non ottengo altri messaggi di errore ma il modulo non è ancora valido.

Il mio modulo ha un campo prodotto come campo base e un pulsante di invio. All'interno del fieldset prodotto ho un campo ID, un campo nome, un campo prezzo e un campo marca. All'interno del mio Brand Fieldset ho un campo id. Come questo:

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, 
       ), 
     )); 
    } 
} 

Questa è la mia nuova dichiarazione Form nella addAction:

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

All'interno del mio modello 'Prodotto' ho inputFilters, filtro richiesto per campo 'id', filtro richiesto per campo 'nome'. E per il campo Marca ho creato altri inputFilter e ha aggiunto al inputFilter principale:

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

Il comportamento bizzarro è che il mio editAction funziona bene e ha la stessa logica.

C'è qualche forma di echo di un messaggio di errore interno dal modulo, qualcosa che mi aiuta a capire PERCHÉ il modulo non è valido.

EDIT 2013-06-01

Ecco il mio pieno controllo:

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

Fornire i metodi completi del controller. – netiul

+0

OK, fornirò l'azione di aggiunta e modifica del controller. –

+0

qualche idea? nessuno? –

risposta

0

Beh, ho avuto la risposta: D

Questo è come l'addAction dovrebbe essere:

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, 
    )); 
} 

A quanto pare ho bisogno di legare e svuotare oggetto oduct al modulo per poter passare la convalida isValid(). Successivamente recupero un oggetto prodotto da $ form-> getData().

0

si può anche fare: $form->setBindOnValidate(false);

0

Il mio caso era passai filtro di ingresso sbagliato. isValid restituisce false, ma $form->getMessages() è vuoto.Modulo OrderForm ha avuto il seguente:

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

Quando ho cambiato UserInputFilter a OrderInputFilter funziona.

Problemi correlati