2013-04-05 1 views
6

양식 컬렉션에서 빈 레코드를 필터링하는 방법을 알아 내려고하고 있습니다. 내 신청서에는 CompetitionLeague의 두 엔티티가 있습니다. 경쟁은 0 개 이상의 리그를 가질 수 있습니다.ZF2 양식 컬렉션 - 빈 레코드 필터링

그래서 Competition 양식 (CompetitionForm), Competition 필드 세트 (CompetitionFieldset) 및 League 필드 세트 (LeagueFieldset)를 만듭니다.

CompetitionFieldset

CompetitionForm 추가되고 CompetitionFieldset 사용자가 하나 개 이상의 리그를 추가 할 수 있도록 Zend\Form\Element\Collection를 이용한다. 아래 각 클래스에 대한 현재 코드를 추가했습니다. 기본적으로

, 나는 내가 Zend\Form\Element\Collection 항목을 추가 할 때, 나는 3

에 카운트 옵션을 설정하십시오 CompetitionFieldset 이내에, 경쟁에서 3 개 리그에 대한 입력 필드를 표시 할 그러나 사용자는 '아무튼 경우 리그를위한 자료를 제공하지 않는다. 나는 그것을 무시하고 싶다. 현재, 3 개의 비어있는 연관 리그가 데이터베이스 내에서 생성됩니다.

예를 들어 name 필드를 만들려면 LeagueFieldsetInputFilter을 설정하면 양식이 유효성을 검사하지 않습니다.

나는 아마 또한 내가 나의 실체를 모델링 등 내 형태를 수화 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

리그 모델에서 필드가 비어 있는지 확인한 후 리그 엔트리를 반복 실행하여 해당 체크를 실행하는 DB에 제출할 수있는 메소드를 작성할 수 있습니다. 비어있는 경우 저장하지 마십시오. 빈 필드 세트에는 여전히 유효성 검사기가 있고 필터가 실행 되었기 때문에 수표를 넣는 가장 효율적인 장소가 아닐 수도 있지만 작동 할 수 있습니다. –

+0

컨트롤러에 관련 코드를 게시하고 교리 개체에 연결을 게시 할 수 있습니까? – YRM

답변

0

당신은 당신의 EntityManager에 전달하기 전에 요청에서 데이터를 가져 오는 '것을 필요 확실히. 빈 레코드를 필터링하기 위해 직접 검증을 추가하십시오. 제출 양식 이벤트를 포착하고 빈 필드를 제출하기 전에 javascript 필터를 제공 할 수도 있습니다.