2013-05-31 2 views
7

मुझे अपने सीआरयूडी अनुप्रयोग में ऐडएक्शन के साथ कोई समस्या है। नियंत्रक पर तर्क $ form-> isValid() सत्यापन पास नहीं करता है लेकिन फ़ॉर्म कोई त्रुटि संदेश नहीं दिखाता है।

मैं के साथ इस (धन्यवाद सैम) की कोशिश की:

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

यह केवल नाम और फ़ील्ड का मान नहीं, बल्कि त्रुटि संदेश दिखाई।

मैंने फ़ॉर्म को पूरी तरह से रिक्त करने की कोशिश की है और यह "संदेश आवश्यक है और खाली नहीं हो सकता" त्रुटि संदेश है, लेकिन फिर, मैं प्रत्येक फ़ील्ड को एक-एक करके भरता हूं जब तक मुझे अधिक त्रुटि संदेश नहीं मिलते हैं लेकिन फॉर्म अभी भी अमान्य है।

मेरे फॉर्म में एक उत्पाद फ़ील्डसेट बेस फ़ील्ड और सबमिट बटन के रूप में है। उत्पाद फ़ील्डसेट के अंदर मेरे पास एक आईडी फ़ील्ड, एक नाम फ़ील्ड, एक मूल्य फ़ील्ड और एक ब्रांड फ़ील्डसेट है। मेरे ब्रांड फ़ील्डसेट के अंदर मेरे पास एक आईडी फ़ील्ड है। इस तरह:

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 में अपने नए फार्म कथन है:

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

मेरे मॉडल 'उत्पाद' के अंदर मेरे पास इनपुट फ़िल्टर है, 'आईडी' फ़ील्ड के लिए आवश्यक फ़िल्टर, 'नाम' फ़ील्ड के लिए आवश्यक फ़िल्टर। और ब्रांड क्षेत्र के लिए मैं अन्य inputFilter बनाया है और मुख्य inputFilter को यह कहा:

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

अजीब व्यवहार है कि मेरे editAction ठीक काम करता है और एक ही तर्क है।

क्या यह फ़ॉर्म से आंतरिक त्रुटि संदेश को प्रतिबिंबित करने का कोई भी रूप है, जो मुझे समझने में मदद करता है कि फ़ॉर्म वैध क्यों नहीं है।

संपादित 2013-06-01

यहाँ मेरा पूरा नियंत्रक:

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

ठीक है, मैं इस सवाल का जवाब मिल गया है: डी

यह कैसे 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, 
    )); 
} 

जाहिर है मुझे पीसने और खाली करने की आवश्यकता है isValid() सत्यापन को पारित करने में सक्षम होने के लिए प्रपत्र को ऑब्जेक्ट करें। उसके बाद मैं $ form-> getData() से एक उत्पाद ऑब्जेक्ट पुनर्प्राप्त करता हूं।

0

तुम भी कर सकते हैं: $form->setBindOnValidate(false);

0

मेरे मामले मैं गलत इनपुट फ़िल्टर पारित कर दिया था। isValid झूठा रिटर्न देता है, लेकिन $form->getMessages() खाली है।

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

जब मैं OrderInputFilter यह काम करता है के लिए UserInputFilter बदल: फार्म OrderForm निम्नलिखित था।

संबंधित मुद्दे