基于其他表单元素的表单元素验证

时间:2013-04-20 00:23:27

标签: php forms zend-framework2 validation

我正在渲染一个表单,将类(课程)添加到数据库中。该课程有一定的开始时间和结束时间。两者都是时间字段。我为该类创建了一个字段集:

<?php
namespace Admin\Form;

use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterProviderInterface;

class ArtClassFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct()
    {
        parent::__construct('artclass');

        $this->add(array(
            'name'          => 'dayofweek',
            'type'          => 'Zend\Form\Element\Select',
            'options'       => array(
                'label'             => 'Day of week:',
                'value_options'     => array(
                    1           => 'Monday',
                    2           => 'Tuesday',
                    3           => 'Wednesday',
                    4           => 'Thursday',
                    5           => 'Friday',
                    6           => 'Saturday',
                ),
            ),
        ));

        $this->add(array(
                'name' => 'starttime',
                'type' => 'Zend\Form\Element\Time',
                'options' => array(
                    'label' => 'Start time:',
                    'format' => 'H:ia',
                ),
            )
        );

        $this->add(array(
                'name' => 'endtime',
                'type' => 'Zend\Form\Element\Time',
                'options' => array(
                    'label' => 'End time:',
                    'format' => 'H:ia',
                ),
            )
        );

        $this->add(array(
                'name' => 'teacher',
                'type' => 'Admin\Form\TeacherSelectorFieldset',
                'options' => array(
                    'label' => 'Teacher:',
                )
            )
        );
    }

    public function getInputFilterSpecification()
    {
        return array(
                'dayofweek' => array(
                    'required' => true,
                    'filters' => array(
                        array('name' => 'Int'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'Between',
                            'break_chain_on_failure' => true,
                            'options' => array(
                                'min' => 1,
                                'max' => 6, 
                            ),
                        ),
                    ),
                ),
                'starttime' => array(
                    'required' => true,
                ),
                'endtime' => array(
                    'required' => true,
                ),
                'teacher' => array(
                ),
        );
    }
}

在我的Form类中,我只需将此字段集添加到我的表单中:

<?php 
namespace Admin\Form;

use Zend\Form\Form;

class ArtClassAdd extends Form
{
    public function __construct()
    {
        parent::__construct("artclass-add");
        $this->setAttribute('action', '/admin/artclass/add');
        $this->setAttribute('method', 'post');

        $this->add(array(
                'type' => 'Admin\Form\ArtClassFieldset',
                'options' => array('use_as_base_fieldset' => true)
            )
        );

        $this->add(array(
                'name' => 'submit',
                'attributes' => array(
                    'type' => 'submit',
                    'value' => 'Save'                   
                )
            )
        );
    }
}

两个时间字段的格式是'H:ia',这意味着我将获得类似'11:00am'的内容。我现在要做的是验证启动时间是否在结束时间之前。问题是我该怎么做?我想我应该使用Zend \ Validator \ Callback,但不确定。

1 个答案:

答案 0 :(得分:2)

你走在正确的轨道上。您必须使用Callback验证程序。对endtime输入规范使用类似的内容:

return array(
    'endtime' => array(
        'required' => true,
        'validators' => array(
            array(
                'name' => 'Callback',
                'options' => array(
                    'callback' => function($value, $context)
                    {
                        $endtime = DateTime::createFromFormat'H:ia', $value);
                        $starttime = DateTime::createFromFormat('H:ia', $context['starttime']);
                        return $endtime > $starttime;
                    }
                ),
            ),
        ),
    ),
);