2013-12-20 3 views
0

Zend \ Form \ Element \ Collection을 사용하여 '행'이 동적으로 추가 된 양식이 있습니다. 이 잘 작동하지만 이러한 행에 대한 유효성 검사를 추가하려면 고군분투하고 있습니다.Zend Form Element Collection 유효성 검사

지금까지 내 코드는 다음과 유사합니다.

<?php 

class EditForm extends \Zend\Form\Form 
{ 
    public function __construct() 
    { 
     parent::__construct('edit'); 
     $this->setUpFormElements(); 
     $this->setupInputFilters(); 
    } 

    protected function setUpFormElements() 
    { 
     $fieldset = new \Zend\Form\Fieldset; 

     $nameElement = new \Zend\Form\Element\Text('name'); 
     $fieldset->add($nameElement); 

     $descriptionElement = new \Zend\Form\Element\Text('description'); 
     $fieldset->add($description); 

     $this->add(
      array(
       'type' => 'Zend\Form\Element\Collection', 
       'name' => 'rows', 
       'options' => array(
        'label' => 'Edit Rows', 
        'should_create_template' => true, 
        'allow_add' => true, 
        'target_element' => $fieldset, 
       ) 
      ) 
     ); 

     return $this; 
    } 

    public function setupInputFilters() 
    { 
     $filter = new \Zend\InputFilter\InputFilter(); 

     $filter->add(
      array(
       'name' => 'rows', 
       'required' => true, 
       'validators' => array(
        // Not sure what to do here! 
       ) 
      ) 
     ); 

     return $this; 
    } 
} 

답변

0

나는 당신이있는 필드 셋의 getInputFilterSpecification 방법에 입력 필터를 추가 할 필요가 있다고 생각 : 나는 InputFilter \ InputFilter에 뭔가를 전달하는 :: (추가)하지만 무엇을 알아낼 수 없습니다 필요 추정 양식에 동적으로 다음

class Row extends Fieldset implements InputFilterProviderInterface 
{ 

     $this->add(array(
      'name' => 'yourFieldset', 
      'options' => array(
       'label' => 'One of your fieldset elements' 
      ), 
      'attributes' => array(
       'required' => 'required' 
      ) 
     )); 


     $this->add(array(
      'name' => 'fields', 
      'options' => array(
       'label' => 'Another fieldset element' 
      ), 
      'attributes' => array(
       'required' => 'required' 
      ) 
     )); 

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

를 추가하면 유효성 검사 그룹

class EditForm extends Form 
{ 
    public function __construct() 
    { 
     $this->add(
     array(
      'type' => 'Zend\Form\Element\Collection', 
      'name' => 'rows', 
      'options' => array(
       'label' => 'Edit Rows', 
       'should_create_template' => true, 
       'allow_add' => true, 
       'target_element' => $fieldset, 
      ) 
      ) 
     ); 

     $this->setValidationGroup(array(
      'csrf', 
      'row' => array(
       'yourFieldset', 
       'fields', 
      ) 
     )); 
    } 
} 
을 설정해야
관련 문제