如何验证 ZF2 中的复选框 [英] How to validate a checkbox in ZF2
本文介绍了如何验证 ZF2 中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经阅读了许多针对 Zend Framework 缺少默认复选框验证的解决方法.
I've read numerous workarounds for Zend Framework's lack of default checkbox validation.
我最近开始使用 ZF2,但那里的文档有点缺乏.
I have recently started using ZF2 and the documentation is a bit lacking out there.
有人可以演示我如何使用 Zend 表单和验证机制验证复选框以确保它被选中吗?我正在为我的表单使用数组配置(使用 ZF 网站上示例应用程序中的默认设置).
Can someone please demonstrate how I can validate a checkbox to ensure it was ticked, using the Zend Form and Validation mechanism? I'm using the array configuration for my Forms (using the default set-up found in the example app on the ZF website).
推荐答案
试试这个
表单元素:
$this->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'agreeterms',
'options' => array(
'label' => 'I agree to all terms and conditions',
'use_hidden_element' => true,
'checked_value' => 1,
'unchecked_value' => 'no'
),
));
在过滤器中,添加数字验证
In filters, add digit validation
use Zend\Validator\Digits; // at top
$inputFilter->add($factory->createInput(array(
'name' => 'agreeterms',
'validators' => array(
array(
'name' => 'Digits',
'break_chain_on_failure' => true,
'options' => array(
'messages' => array(
Digits::NOT_DIGITS => 'You must agree to the terms of use.',
),
),
),
),
)));
这篇关于如何验证 ZF2 中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文