1
我有一些在Zend 2表单中设置的验证程序,但isValid总是返回true,忽略它们。倾倒整个窗体对象,它看起来并不像验证甚至连接,这里的表单代码:Zend 2表单验证程序
namespace UserManagement\Form;
use Zend\Form\Form;
class SearchUserForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('SearchUser');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'search',
'attributes' => array(
'type' => 'text',
'required' => true,
),
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 4,
'max' => 100,
),
)
),
));
控制器
然后我检查它是否是有效的:
if($this->getRequest()->isPost())
{
$searchForm->setData($this->params()->fromPost());
if($searchForm->isValid())
{
echo "yep";exit;
}
else
{
echo "nope";exit;
}
始终输出“ yep'尽管有1个字符的字符串长度。我实际上已经得到了这个工作,但将验证器放在单独的过滤器类中,并将其附加到窗体上 - 但我的问题是应该这个工作?
添加字段的形式和过滤器,验证设置为它们是两个不同的东西。检查这个参考 - http://framework.zend.com/manual/2.3/en/user-guide/forms-and-actions.html你可以随时在Form类中设置输入过滤器,但它的定义应该是分开的向表单添加字段。 – 2014-09-02 08:59:16