2016-06-08 2 views
0

Я пытаюсь выяснить, как выполнить проверку в полях формы, где имена полей находятся в массиве. Имена полей, которые у меня возникли проблемы с в этом формате: item[1][urgent] Форма поле приходит через и здесь формат данные только до ввода его через для проверки:Zend 2 Проверка формы - Arrayed Field Names

'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, кампус), но у меня возникли проблемы с получением валидации в пределах 'item' array. Есть ли хороший способ проверить, как я это сделал? Вот соответствующий код:

Форма:

$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

Существует специальный Zend\Form\Element\Collection класс для таких элементов массива.

Проверьте наличие документации полного ZF2 этого класса глава Collection

+0

Это то, что я искал! благодаря – adamst85

Смежные вопросы