Zend 2表单验证 - 阵列字段名称

时间:2016-06-08 02:17:04

标签: php zend-framework2 zend-form zend-filter

我正在尝试研究如何在字段名称在数组中的表单字段上执行验证。我遇到问题的字段名称采用以下格式:item[1][urgent]  表单字段通过,这里是数据通过验证之前的格式:

  'idNumber' => string '' (length=0)
  'phone' => string '' (length=0)
  'campus' => string '10427' (length=5)
  'item' => 
    array (size=1)
      1 => 
        array (size=6)
          'ILLType' => string 'book' (length=4)
          'requiredBy' => string '' (length=0)
          'urgent' => string '0' (length=1)
          'citation' => string '' (length=0)
          'getFrom' => string '' (length=0)
          'copyright' => string '0' (length=1)
  'captcha' => 
    array (size=2)
      'id' => string 'f0b53b625adad9371eafb7ee0b2e171b' (length=32)
      'input' => string '' (length=0)
  'submit' => string 'Submit ILL' (length=10)

我对基础中的表单字段(即 idNumber,校园)没有任何问题,但在“项目”数组中进行验证时遇到问题。有没有一种很好的方法来验证我的方式?以下是相关代码:

形式:

$idNumber = new Element\Text('idNumber');
$idNumber->setLabel('* Your Deakin Library Borrower Number')
                            ->setLabelAttributes(array('class' => 'sq-form-question-title'))
                            ->setAttribute('summary', '(14 digit barcode number at the bottom of student/staff ID):')
                            ->setAttribute('class', 'sq-form-field required')
                            ->setAttribute('id', 'idNumber');



$ILLType = new Element\Select('item[1][ILLType]');
$ILLType->setLabel('* What is your request about?')
                ->setLabelAttributes(array('class' => 'sq-form-question-title'))
                ->setAttribute('class', 'sq-form-field required request_type')
                ->setAttribute('id', 'ILLType_1')
                //->setAttribute('multiple', 'multiple')
                ->setOptions($ILLTypes);



$urgent = new Element\Checkbox('item[1][urgent]');
$urgent->setLabel('Urgently required?')
        ->setLabelAttributes(array('class' => 'sq-form-question-title'))
        ->setAttribute('class', 'sq-form-field')
        ->setAttribute('id', 'urgent_1');

表单过滤器:

$idNumber = new Input('idNumber');
$idNumber->getValidatorChain()
      ->addByName('NotEmpty');                


$ILLType = new Input('item[1][ILLType]');
$ILLType->getValidatorChain()
        ->addByName('InArray', array('haystack' => array_keys(
                                    $ILLTypes['options']
                                )));
$ILLType->getFilterChain()
         ->attach(new Filter\StringToLower())
         ->attachByName('StripTags');

PostController中:

        $this->ILLForm->prepareElements($this->ILLCategories, $this->campuses, $this->getFromOptions);
        // Assign POST data to form
        $this->ILLForm->setData($data);  

        $this->ILLFormFilter->prepareFilters($this->ILLCategories, $this->campuses, $this->getFromOptions);
        $this->ILLForm->setInputFilter($this->ILLFormFilter);

        if (!$this->ILLForm->isValid($data)) {

            $this->flashMessenger()->addMessage('Please ensure you have filled in all the required fields');
        }

1 个答案:

答案 0 :(得分:1)

此类数组元素有一个特殊的Zend\Form\Element\Collection类。

Collection

一章中查看此课程的完整ZF2文档