不是您问题的完整答案,但由于注释没有语法格式;这是一个过滤器,您可以使用它来使您的字段值为空(如果为空)。
class My_Filter_NullIfEmpty implements Zend_Filter_Interface
{
public function filter( $value )
{
// maybe you need to expand the conditions here
if( 0 == strlen( $value ) )
{
return null;
}
return $value;
}
}
关于所需部分:我不确定。您可以尝试在 Nabble 上搜索 ZF 邮件列表:
http://www.nabble.com/Zend-Framework-Community-f16154.html
或者订阅他们的邮件列表,然后问他们这个问题。通过 Nabble,或直接通过 framework.zend.com 上的地址:http:
//tinyurl.com/y4f9lz
编辑:好的,所以现在我自己做了一些测试,因为你所说的一切对我来说听起来都违反直觉。你的例子很适合我。这是我用过的:
<?php
class Form extends Zend_Form
{
public function init()
{
$title = new Zend_Form_Element_Text('title', array(
'label' => 'Title',
'required' => false,
'filters' => array(
'StringTrim',
'HtmlEntities',
'NullIfEmpty' // be sure this one is available
),
'validators' => array(
array('StringLength', false, array(3, 100))
),
));
$this->addElement( $title );
}
}
$form = new Form();
$postValues = array( 'title' => '' ); // or
$postValues = array( 'title' => ' ' ); // or
$postValues = array( 'title' => 'ab' ); // or
$postValues = array( 'title' => ' ab ' ); // or
$postValues = array( 'title' => '<abc>' ); // all work perfectly fine with me
// validate the form (which automatically sets the values in the form object)
if( $form->isValid( $postValues ) )
{
// retrieve the relevant value
var_dump( $form->getValue( 'title' ) );
}
else
{
echo 'form invalid';
}
?>