phpzend-framework2zend-formzend-filter

Zend 2 Form Validation - Arrayed Field Names


I am trying to work out how to perform validation on form fields where the field names are in an array. The field names that I am having trouble with are in this format: item[1][urgent] The form fields come through and here is the format of the data just before it is put through for validation:

  '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)

I have no issues with the form fields in the base (ie. idNumber, campus) but am having trouble getting validation within the 'item' array. Is there a good way of validating the way I have done it? Here is the relevant code:

Form:

$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');

Form Filter:

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

Solution

  • There is a special Zend\Form\Element\Collection class for such array elements.

    Check for the full ZF2 documentation on this class the chapter Collection