dongxiusuo9881 2013-04-20 00:23
浏览 28
已采纳

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

I'm rendering a form to add a class (course) to a database. The class has a certain starttime and endttime. Both are time of day fields. I created a fieldset for the class:

<?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(
                ),
        );
    }
}

In my Form class I simply add this fieldset to my 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'                   
                )
            )
        );
    }
}

The format of the two time fields is 'H:ia' so that means I will get something like '11:00am'. What I would like to do now is to validate that the starttime is before the endtime. The question is how do I do that? I'm thinking I should probably use Zend\Validator\Callback, but not sure.

  • 写回答

1条回答 默认 最新

  • droos02800 2013-04-20 00:46
    关注

    You're on the right track. You have to use the Callback validator. Use something like this for the endtime input spec:

    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;
                        }
                    ),
                ),
            ),
        ),
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?