我的表单中有一个字段名称topic_name [],我的表单代码位于
之下 ?php
namespace Tutorials\Form;
use Zend\Captcha;
use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\Form\Form;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
class AddTopicForm extends Form {
public $fieldData;
public function __construct($data = array()){
parent::__construct('AddTopic');
$this->setAttribute('class', 'form-horizontal');
$this->setAttribute('novalidate', 'novalidate');
$this->add(array(
'name' => 'topic_name[]',
'attributes' => array(
'type' => 'text',
'id' => 'topic_name',
'class' => 'form-control',
'value' => ''
),
'options' => array(
'label' => ' Topic Name',
),
));
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
'id' => 'id'
)
));
$button = new Element('add_more');
$button->setValue('+AddMore');
$button->setAttributes(array(
'type' => 'button',
'id'=>'add_more',
'class'=>'btn btn-info'
));
$save = new Element('save');
$save->setValue('Save');
$save->setAttributes(array(
'type' => 'submit',
'id'=>'save',
'class'=>'btn btn-info'
));
$reset = new Element('reset');
$reset->setValue('Reset');
$reset->setAttributes(array(
'type' => 'reset',
'id'=>'reset',
'class'=>'btn'
));
$this->add($button);
$this->add($save);
$this->add($reset);
}//end of function_construct.
}//end of registration form class.
创建此表单后,我使用相同topic_name []的jquery添加更多字段,我将获得如下数据
在帖子值中,我得到的值如下
Array
(
[topic_name] => Array
(
[0] => gdfgdfg
[1] => dfgdfg
[2] => gfhg
[3] => gfhfg
[4] => jhgjhj
[5] => hjhjgh
[6] => jghjghj
)
[id] =>
[save] => Save
)
之后我使用验证器验证此数据
<?php
namespace Tutorials\Form;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Zend\I18n\Validator\Alpha;
class AddTopicFilterForm implements InputFilterAwareInterface {
protected $inputFilter;
public $fieldData;
function __construct($data = array()) {
if(!empty($data['data']['topic_name'])) {
$this->fieldData = $data['data']['topic_name'];
}
}
public function setInputFilter(InputFilterInterface $inputFilter){
throw new \Exception("Not used");
}
public function getInputFilter(){
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
//for($i = 0; $i < count($this->fieldData); $i++) {
$inputFilter->add($factory->createInput(array(
'name' => 'topic_name[]',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'Alpha',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));
//}
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
但我没有收到任何错误,也没有任何错误消息重定向回到表单我知道它没有验证我的数据。我通过一个循环尝试了这个但是得到了空字段的错误消息。是否有人知道如何验证我的数据如上所述?
我有另一个问题,我可以在我的文件名中使用[],例如topic_name [] ??我问这个,因为我已经尝试验证这个单值,但得到相同的错误消息“值是必需的,不能为空”