2013-04-05 8 views
6

मैं एक फॉर्म संग्रह से खाली रिकॉर्ड्स को फ़िल्टर करने का तरीका बनाने का प्रयास कर रहा हूं। मेरे आवेदन के साथ मेरे पास 2 इकाइयां हैं, Competition और League। एक प्रतियोगिता में शून्य या अधिक लीग हो सकती है।जेडएफ 2 फॉर्म संग्रह - खाली रिकॉर्ड्स फ़िल्टर करना

तो मैं एक प्रतियोगिता फार्म (CompetitionForm), एक प्रतियोगिता fieldset (CompetitionFieldset) और एक लीग fieldset (LeagueFieldset) पैदा करते हैं।

CompetitionFieldsetCompetitionForm में जोड़ा जाता है, और CompetitionFieldsetZend\Form\Element\Collection का उपयोग करता है उपयोगकर्ता 1 या अधिक लीग को जोड़ने के लिए अनुमति देने के लिए। मैंने नीचे प्रत्येक वर्ग के लिए वर्तमान कोड जोड़ा है।

डिफ़ॉल्ट रूप से, मैं, एक प्रतियोगिता के भीतर 3 लीग के लिए इनपुट फ़ील्ड प्रदर्शित करने के CompetitionFieldset के भीतर ऐसा है, जब मैं Zend\Form\Element\Collection आइटम जोड़ने, मैं 3.

को गिनती विकल्प सेट चाहते हैं लेकिन कोई उपयोगकर्ता नहीं करता है, तो ' लीग के लिए किसी भी डेटा की आपूर्ति नहीं, मैं उन्हें अनदेखा करना चाहता हूं। वर्तमान में, मेरे डेटाबेस में तीन खाली जुड़े लीग बनाए गए हैं।

यदि LeagueFieldset पर फ़ील्ड को फ़ील्ड को उदाहरण के लिए आवश्यक बनाने के लिए InputFilter सेट किया गया है, तो फॉर्म मान्य नहीं होगा।

मैं शायद यह भी है कि मैं Doctrine2 उपयोग कर रहा हूँ मेरी संस्थाओं मॉडल और मेरी रूपों आदि हाइड्रेट करने के लिए उल्लेख करना चाहिए

मुझे यकीन है कि मैं इसे में अपने मॉडल पर कुछ अतिरिक्त कोड के साथ काम करते हैं, या यहाँ तक कि कर सकता हूँ मेरी नियंत्रक जहां मैं फॉर्म को संसाधित करता हूं, लेकिन मुझे यकीन है कि फिल्टर का उपयोग कर एक नीदर समाधान हो सकता है।

यदि कोई मुझे सही दिशा में इंगित कर सकता है तो इसकी बहुत सराहना की जाएगी।

आपके द्वारा प्रदान की जा सकने वाली किसी भी सहायता के लिए अग्रिम धन्यवाद।

:wq 
familymangreg 

मेरे CompetitionForm

use Kickoff\Form\AbstractAdminForm; 
use Doctrine\Common\Persistence\ObjectManager; 
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator; 
use Zend\Stdlib\Hydrator\ClassMethods; 

class CompetitionForm extends AbstractAdminForm 
{ 
    public function __construct(ObjectManager $objectManager) 
    { 
     parent::__construct($objectManager, 'competition-form'); 

     $fieldset = new CompetitionFieldset($objectManager); 
     $fieldset->setUseAsBaseFieldset(true); 
     $this->add($fieldset); 

     $this->add(array(
      'name' => 'submit', 
      'attributes' => array(
       'type' => 'submit', 
       'value' => 'Submit', 
       'id' => 'submitbutton', 
      ), 
     )); 
    } 
} 

मेरे CompetitionFieldset

use Kickoff\Form\AbstractFieldset; 
use Kickoff\Model\Entities\Competition; 
use Doctrine\Common\Persistence\ObjectManager; 
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator; 

class CompetitionFieldset extends AbstractFieldset 
{ 
    public function __construct(ObjectManager $objectManager) 
    { 
     parent::__construct($objectManager,'Competition'); 

     $this->setHydrator(new DoctrineHydrator($this->objectManager,'Kickoff\Model\Entities\Competition')) 
      ->setObject(new Competition()); 

     $this->setLabel('Competition'); 

     $this->add(array(
      'name' => 'name', 
      'options' => array(
       'label' => 'Competition name (e.g. Premier League)',     
      ), 
      'attirbutes' => array(
       'type' => 'text', 
      ), 
     )); 

     $this->add(array(
      'name' => 'long_name', 
      'options' => array(
       'label' => 'Competition long name (e.g. Barclays Premier League)', 
      ), 
      'attirbutes' => array(
       'type' => 'text', 
      ), 
     )); 

     $leagueFieldset = new LeagueFieldset($objectManager); 
     $this->add(array(
      'type' => 'Zend\Form\Element\Collection', 
      'name' => 'leagues', 
      'options' => array(
       'label' => 'Leagues', 
       'count' => 3, 
       'should_create_template' => true, 
       'allow_add' => true, 
       'target_element' => $leagueFieldset, 
      ), 
     )); 
    } 
} 

मेरे LeagueFieldset

use Kickoff\Form\AbstractFieldset; 
use Kickoff\Model\Entities\League; 
use Doctrine\Common\Persistence\ObjectManager; 
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator; 
use Zend\InputFilter\InputFilterProviderInterface; 

class LeagueFieldset extends AbstractFieldset implements InputFilterProviderInterface 
{ 
    public function __construct(ObjectManager $objectManager) 
    { 
     parent::__construct($objectManager,'League'); 

     $this->setHydrator(new DoctrineHydrator($this->objectManager,'Kickoff\Model\Entities\League')) 
      ->setObject(new League()); 

     $this->setLabel('League'); 

     $this->add(array(
      'name' => 'name', 
      'options' => array(
       'label' => 'League name (e.g. First Qualifying Round)', 
      ), 
      'attirbutes' => array(
       'type' => 'text', 
      ), 
     )); 

     $this->add(array(
      'name' => 'long_name', 
      'options' => array(
       'label' => 'League long name (e.g. UEFA Champions League First Qualifying Round)', 
      ), 
      'attirbutes' => array(
       'type' => 'text', 
      ), 
     )); 
    } 

    public function getInputFilterSpecification() 
    { 
     return array(
      'name' => array(
       'required' => true, 
      ) 
     ); 
    } 

} 
+0

आप लीग मॉडल में एक विधि लिख सकते हैं जो जांचता है कि फ़ील्ड खाली हैं या नहीं, तो लीग प्रविष्टियों को फिर से जांचने से पहले डीबी रन में जमा करने से पहले। यदि यह खाली है, तो इसे बचाओ मत। हो सकता है कि चेक को रखने के लिए सबसे कुशल जगह न हो क्योंकि रिक्त फ़ील्ड में अभी भी वैधताएं होंगी और फ़िल्टर उस पर चलेंगे लेकिन यह काम करेगा। –

+0

क्या आप अपने कंट्रोलर में प्रासंगिक कोड और अपनी सिद्धांत संस्थाओं में एसोसिएशन पोस्ट कर सकते हैं? – YRM

उत्तर

0

आप अनुरोध से डेटा प्राप्त करने से पहले आप इसे EntityManager के पास है कि 'की जरूरत है निश्चित रूप से एस है। खाली रिकॉर्ड्स को फ़िल्टर करने के लिए बस अपना खुद का सत्यापन जोड़ें। आप जावास्क्रिप्ट फ़िल्टर भी प्रदान कर सकते हैं जो सबमिट फॉर्म ईवेंट को पकड़ लेगा और सबमिट करने से पहले खाली फ़ील्ड हटा देगा।

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