我想允许在表单上的某些字段中输入空。我的表单类在一个文件中,其过滤器在另一个文件中。筛选器如下所示
class ReportFilter extends InputFilter
{
public function __construct()
{
$this->add(array(
'name' => 'field_1',
'required' => false,
'allow_empty' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
));
$this->add(array(
'name' => 'field_2',
'required' => false,
'allow_empty' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
));
问题是,即使 required 设置为 false 并且 allow_empty 设置为 true,当提交表单时,这些字段中没有任何内容,验证也会失败,并且它们被标记为消息"值是必需的,不能为空"。我错过了什么?
你试过这个符号吗?
...
'allowEmpty'=>true, //insetad of allow_empty
...
版本说明:尝试从过滤器选项中拆分元素声明,如下例所示:
use ZendFormForm;
use ZendInputFilterInputFilter;
use ZendInputFilterInputFilterInterface;
use ZendInputFilterFactory as InputFactory;
class ReportFilter extends InputFilter
{
public function __construct()
{
$this->add(array(
'name' => 'field_1',
'type' => 'ZendFormElementText',
));
}
public function getInputFilter() {
if (!$this->filter) {
$inputFilter = new InputFilter();
$factory = new InputFactory ();
$inputFilter->add($factory->createInput(array(
'name' => 'field_1',
'required' => false,
'allowEmpty' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
)));
$this->filter = $inputFilter;
}
return $this->filter;
}
public function setInputFilter(InputFilterInterface $inputFilter) {
throw new Exception('It is not allowed to set the input filter');
}
}
根据官方文档,use validators "not_empty"
use ZendInputFilterFactory;
$factory = new Factory();
$inputFilter = $factory->createInputFilter(array(
'password' => array(
'name' => 'password',
'required' => true,
'validators' => array(
array(
'name' => 'not_empty',
),
array(
'name' => 'string_length',
'options' => array(
'min' => 8
),
),
),
),
));
$inputFilter->setData($_POST);
echo $inputFilter->isValid() ? "Valid form" : "Invalid form";
奖金
Zend Framework 2 为你的表单提供了很多工具。
这是Fieldset
和InputFilterProviderInterface
的一个很好的例子。如果您使用Doctrine,您还可以使用"Hydrator"将表单自动绑定到您的实体中。有了这段代码,没问题required => false
就足够了,并允许空字符串
class AddressFieldset extends Fieldset implements InputFilterProviderInterface {
public function __construct(ServiceManager $serviceManager) {
parent::__construct('attribute');
// $em = $serviceManager->get('DoctrineORMEntityManager');
// $this->setHydrator(new DoctrineHydrator($em, 'MyProject...EntityAddressEntity'))->setObject(new AddressEntity());
$this->add(array(
'name' => 'id',
'type' => 'ZendFormElementHidden',
'attributes' => array(
'autocomplete' => 'off'
)
));
$this->add(array(
'name' => 'name',
'options' => array(
'label' => 'Address'
),
'attributes' => array(
'class' => 'span3',
'autocomplete' => 'off'
)
));
$this->add(array(
'name' => 'district',
'options' => array(
'label' => 'District'
)
));
$this->add(array(
'name' => 'postal_code',
'options' => array(
'label' => 'Postal code'
)
));
$this->add(array(
'name' => 'city',
'options' => array(
'label' => 'City'
)
));
}
public function getInputFilterSpecification() {
return array(
'name' => array(
'required' => false,
),
'address' => array(
'required' => true,
),
'district' => array(
'required' => false,
),
'postal_code' => array(
'required' => true,
),
'city' => array(
'required' => true,
)
);
}
}
然后创建一个包含字段集的表单
官方文档中非常好的示例,如果您控制表单字段集和水合器(用于您的数据库),您将赢得很多时间。当然,在我的示例中,我指定了是否需要输入,您也可以添加自定义验证器。
http://framework.zend.com/manual/current/en/modules/zend.form.collections.html