如何验证zf2中的空日期值

时间:2015-01-23 18:37:25

标签: forms validation zend-framework2

我有一个包含日期字段的数据集。在数据库中,当添加新行时,字段默认为0000-00-00。在我的fieldset中,元素定义如下:

$this->add(array(
    'name' => 'memberBirthDate',
    'type' => 'Zend\Form\Element\Date',
    'attributes' => array(
        'type'  => 'date',
    ),
    'options' => array(
        'label' => 'birth date',
        'format' => 'n/j/y'
    ),
));

我不希望元素需要才能使表单验证。但是,如果未输入日期,则表单验证会显示错误The input does not appear to be a valid date

此外,格式'n/j/y'将空值呈现为11/30/-1。如果在输入中输入了0,则表单验证会显示错误The input does not appear to be a valid date,如果输入了0/0/00,则验证会同时提供The input does not appear to be a valid dateThe input does not fit the date format 'n/j/y'。< / p>

有没有办法让验证器接受空日期值或跳过未更改输入的验证?

修改

allow_empty不起作用。我已设置它并设置'required' -> false。我甚至检查过确认验证属性是通过回显所有错误和属性一直传递给控制器​​和视图的。以下代码:

// enumerate message
$messages = $form->getMessages();
if($messages) {
  echo "<h4>error messages:</h4>";
  print_r ($messages);
}
echo "<br/>";
echo "allow_empty: " . $memberBirthDate->getAttribute('allow_empty') . "<br/>";
echo "required: " . $memberBirthDate->getAttribute('required') . "<br/>";

产生

Array ( [member-fieldset] => Array ( [memberBirthDate] => Array ( [isEmpty] => Value is required and can't be empty ) ) ) 
allow_empty: 1
required:

编辑2

我在字段集中指定了过滤器。我项目的这一部分遵循http://framework.zend.com/manual/current/en/modules/zend.form.collections.html所呈现的格式,并未提及有关使用工厂的任何建议。我的过滤器是这样的:

namespace AdminMembers\Form;

use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;

use AdminMembers\Entity\Member;

class MemberFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('member-fieldset');

        $this->setHydrator(new DoctrineHydrator($objectManager, 'AdminMembers\Entity\Member'))
             ->setObject(new Member());

            // ...

        $this->add(array(
            'name' => 'memberBirthDate',
            'type' => 'Zend\Form\Element\Date',
                'required' => false,
                'allow_empty' => true,
            'attributes' => array(
                'type'  => 'date',
                'required' => false,
                'allow_empty' => true,
            ),
            'options' => array(
                'label' => 'birth date',
                'format' => 'n/j/y'
            ),
        ));

            // ...

    }

    public function getInputFilterSpecification()
    {

            // ...

        return array(
            'memberBirthDate' => array(
                'required' => false,
                'allow_empty' => true,
            ),
        );

            // ...

    }

}

namespace AdminMembers\Form;

use Zend\InputFilter\InputFilter;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Form;

class AddMemberForm extends Form
{
    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('add_member_form');

        $this->setHydrator(new DoctrineHydrator($objectManager, 'AdminMembers\Entity\Member'));

        $adminMemberFieldset = new MemberFieldset($objectManager);
        $adminMemberFieldset->setUseAsBaseFieldset(true);
        $this->add($adminMemberFieldset);

        $this->setAttribute('method', 'post');
        $this->setInputFilter(new InputFilter());

          // ...

        $this->setValidationGroup(array(
            'member-fieldset' => array(
                'memberID',
                    // ...
                'memberBirthDate',
                    // ...
            ),
        ));

    }

}

2 个答案:

答案 0 :(得分:0)

您需要为其添加过滤器。

示例过滤器:

namespace YourNamespace\Filter;

use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

class YourFilter extends InputFilter
{
    public function getInputFilter()
    {
        $this->add(
            array(
                'name' => 'memberBirthDate',
                'required' => true,
            )
        );
    }
}

示例工厂:

namespace YourNamespace\Factory;

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;

class YourFactory extends FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {  
        $form = new \YourNamespace\Form\YourForm();

        $filter = new \YourNamespace\Filter\YourFilter();

        $form->setInputFilter($filter->getInputFilter());

        return $form;
    }
}

在module.config.php上的servicemanager键上注册factory: 您可能应该与旧配置合并:

'service_manager' => array(
    'factories' => array(
        'YourNamespace\Factory\YourFactoryFinalName' => 'YourNamespace\Factory\YourFactory'
    ),
)

随时随地调用服务管理器,带有过滤器的表单你可以自己添加更多的过滤规则。

$this->getServiceLocator()->get('YourNamespace\Factory\YourFactoryFinalName');

额外:创建get / set以便在您需要此工厂的任何地方轻松使用特征:

namespace YourNamespace\Factory;

use YourNamespace\Factory\YourFactory

trait YourFactoryTrait {

    protected $yourFactory;

    public function getYourFactory()
    {
        if (!isset($this->yourFactory)) {
            $this->yourFactory = this->getServiceLocator()->get('YourNamespace\Factory\YourFactoryFinalName');
        }
        return $this->yourFactory;
    }

    public function setYourFactory(YourFactory $yourFactory)
    {
        $this->yourFactory = $yourFactory;
        return $this;
    }
}

您打算使用的类的属性:

use YourNamespace\Factory\YourFactoryTrait;

在方法体中:

$factory = $this->getYourFactory();
$factory->setData($data);

if ($factory->isValid()) {
}

答案 1 :(得分:-1)

您可以配置字段以允许通过输入过滤器的空值。 Rob Allen(akrabat)在allow_empty点击这篇文章:http://akrabat.com/zend-framework-2/setting-up-required-fields-that-can-be-empty-with-zendinputfilter/