2013-04-05 20 views
6

Tôi đang cố gắng tìm ra cách lọc các bản ghi trống từ một bộ sưu tập biểu mẫu. Với đơn đăng ký của tôi, tôi có 2 thực thể, CompetitionLeague. Một cuộc thi có thể có không hoặc nhiều Giải đấu.Bộ sưu tập biểu mẫu ZF2 - lọc các bản ghi trống

Vì vậy, tôi tạo biểu mẫu Cạnh tranh (CompetitionForm), một trường cạnh tranh (CompetitionFieldset) và một fieldset League (LeagueFieldset).

CompetitionFieldset được thêm vào CompetitionFormCompetitionFieldset sử dụng Zend\Form\Element\Collection để cho phép người dùng thêm 1 hoặc nhiều Giải đấu. Tôi đã thêm mã hiện tại cho mỗi lớp bên dưới.

Theo mặc định, tôi muốn hiển thị các lĩnh vực đầu vào cho 3 giải đấu trong một cuộc thi, vì vậy trong CompetitionFieldset, khi tôi thêm Zend\Form\Element\Collection mục, tôi đặt tùy chọn đếm đến 3.

Nhưng nếu người dùng doesn' t cung cấp bất kỳ dữ liệu cho các giải đấu, tôi muốn bỏ qua chúng. Hiện tại, ba giải đấu trống có liên quan được tạo trong cơ sở dữ liệu của tôi.

Nếu tôi đặt InputFilter trên số LeagueFieldset để yêu cầu trường name chẳng hạn, thì biểu mẫu sẽ không xác thực.

tôi nên có lẽ cũng đề cập đến mà tôi đang sử dụng Doctrine2 để mô hình thực thể của tôi và làm ẩm các hình thức của tôi, vv

tôi chắc chắn rằng tôi có thể làm cho nó làm việc với một số mã bổ sung vào mô hình của tôi, hoặc thậm chí trong tôi Bộ điều khiển nơi tôi xử lý biểu mẫu, nhưng tôi chắc chắn có một giải pháp neater có thể sử dụng Bộ lọc.

Nếu có ai có thể chỉ cho tôi đúng hướng, nó sẽ được đánh giá cao.

Rất cảm ơn trước vì bất kỳ trợ giúp nào bạn có thể cung cấp.

:wq 
familymangreg 

CompetitionForm My

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

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

My 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

Bạn có thể viết một phương thức trong mô hình Liên đoàn để kiểm tra xem các trường có trống không khi lặp lại các mục trong Liên minh ngay trước khi gửi cho DB chạy kiểm tra đó. Nếu nó trống, đừng lưu nó. Có lẽ không phải là nơi hiệu quả nhất để đặt kiểm tra vì các ô trống sẽ vẫn có trình xác thực và bộ lọc chạy trên đó nhưng nó sẽ hoạt động. –

+0

bạn có thể đăng các mã có liên quan trong bộ điều khiển của bạn và các hiệp hội trong các thực thể học thuyết của bạn? – YRM

Trả lời

0

Bạn cần phải lấy dữ liệu từ yêu cầu trước khi bạn vượt qua nó để EntityManager, rằng' s chắc chắn. Chỉ cần thêm xác nhận của riêng bạn để lọc các bản ghi trống. Bạn cũng có thể cung cấp bộ lọc javascript sẽ bắt sự kiện gửi biểu mẫu và xóa các trường trống trước khi gửi.

Các vấn đề liên quan