Zend 2 表单验证 - 数组字段名称
Zend 2 Form Validation - Arrayed Field Names
我正在尝试研究如何对字段名称位于数组中的表单字段执行验证。我遇到问题的字段名称采用以下格式: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、campus)没有任何问题,但在 'item' 数组。有没有一种很好的方法来验证我的做法?这是相关代码:
表格:
$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');
后控制器:
$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');
}
对于这样的数组元素有一个特殊的Zend\Form\Element\Collection
class。
查看有关此 class 章节 Collection
的完整 ZF2 文档
我正在尝试研究如何对字段名称位于数组中的表单字段执行验证。我遇到问题的字段名称采用以下格式: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、campus)没有任何问题,但在 'item' 数组。有没有一种很好的方法来验证我的做法?这是相关代码:
表格:
$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');
后控制器:
$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');
}
对于这样的数组元素有一个特殊的Zend\Form\Element\Collection
class。
查看有关此 class 章节 Collection
的完整 ZF2 文档