如何使用zf2中的表单工厂创建字段集集合

时间:2016-10-20 22:48:53

标签: php forms zend-framework2

我尝试创建一个包含仅使用数组规范和Zend\Form\Factory的字段集集合的表单。

以下是我使用工厂创建表单的方法:

$factory = new Zend\Form\Factory();
$fieldset = $factory->createFieldset(array(
    'elements' => array(
        array(
            'spec' => array(
                'name' => 'name',
                'type' => 'Text',
                'attributes' => array(
                    'class' => 'form-control input-sm',
                ),
                'options' => array(
                    'label' => 'Name',
                ),
            ),
        ),
        array(
            'spec' => array(
                'name' => 'driverClass',
                'type' => 'Text',
                'attributes' => array(
                    'class' => 'form-control input-sm',
                ),
                'options' => array(
                    'label' => 'Driver',
                ),
            ),
        ),
    ),
    'input_filter' => array(
        'name' => array(
            'required' => true,
        ),
    ),
));
$form = $factory->createForm(array(
    'name' => 'application-form',
    'attributes' => array(
        'role' => 'form',
    ),
    'elements' => array(
        array(
            'spec' => array(
                'type' => 'Collection',
                'name' => 'connection',
                'options' => array(
                    'label' => 'Connections',
                    'allow_add' => true,
                    'allow_remove' => true,
                    'should_create_template' => true,
                    'count' => 2,
                    'target_element' => $fieldset,
                ),
            ),
        ),
        array(
            'spec' => array(
                'name' => 'security',
                'type' => 'Csrf',
                'attributes' => array(
                    'required' => 'required',
                ),
            ),
        ),
        array(
            'spec' => array(
                'name' => 'submit',
                'type' => 'Submit',
                'attributes' => array(
                    'class' => 'btn btn-sm btn-primary',
                ),
                'options' => array(
                    'label' => 'Apply',
                ),
            ),
        ),
    ),
));

当我尝试设置数据和呈现表单元素时,生成的表单工作正常。但是当我验证它并检索数据时(在控制器中):

$form->setData($this->getRequest()->getPost());

if ($form->isValid() === true) {
    $data = $form->getData();

    var_dump($this->getRequest()->getPost());
    var_dump($data);
}

将这组数据用作POST:

object(Zend\Stdlib\Parameters)[141]
  private 'storage' (ArrayObject) => 
    array (size=3)
      'connection' => 
        array (size=2)
          0 => 
            array (size=2)
              'name' => string 'orm_default' (length=11)
              'driverClass' => string 'Doctrine\DBAL\Driver\PDOMySql\Driver' (length=36)
          1 => 
            array (size=2)
              'name' => string 'blog' (length=4)
              'driverClass' => string 'Doctrine\DBAL\Driver\PDOMySql\Driver' (length=36)
      'submit' => string '' (length=0)
      'security' => string '20d5c146d8874dc804948e962d5de91b-87c9e4097f9140d259efb5c589a05d6b' (length=65)

调用$form->getData()返回的数组显示空集合:

array (size=3)
  'security' => string '20d5c146d8874dc804948e962d5de91b-87c9e4097f9140d259efb5c589a05d6b' (length=65)
  'submit' => string '' (length=0)
  'connection' => 
    array (size=0)
      empty

我错过了什么?

预期结果是一个名为' connection'在此示例中,包含两个数组,表示POST数据指定的两个字段集。我有一种感觉这与丢失的InputFilter(或至少它的规范)有关,因为我实现了一个扩展Zend\Form\Fieldset并实现Zend\InputFilter\InputFilterProviderInterface的字段集类时获得了预期的结果。 / p>

1 个答案:

答案 0 :(得分:0)

刚刚发现了这个类 Public Function MultiplyMatricesParallel(ByVal matA As Double()(), ByVal matB As Double()()) As Double()() Dim matACols As Integer = matA(0).GetLength(0) Dim matBCols As Integer = matB(0).GetLength(0) Dim matARows As Integer = matA.GetLength(0) Dim result(matARows - 1)() As Double For i = 0 To matARows - 1 ReDim result(i)(matBCols - 1) Next Dim tempMat()() As Double = MatrixTranspose(matB) Parallel.For(0, matARows, Sub(i) For j As Integer = 0 To matBCols - 1 Dim temp As Double = 0 Dim maA() As Double = matA(i) Dim maB() As Double = tempMat(j) For k As Integer = 0 To matACols - 1 temp += maA(k) * maB(k) Next result(i)(j) += temp Next End Sub) Return result End Function ,这正是我错过的。

我在字段集规范中添加了一个类型并更改了输入过滤器规范(这是必需的),如下所示:

Zend\Form\InputFilterProviderFieldset

现在它运作正常。希望这有助于某人。