【问题标题】:Form element validation based on other form element基于其他表单元素的表单元素验证
【发布时间】:2013-04-20 00:23:26
【问题描述】:

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

<?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,但不确定。

【问题讨论】:

    标签: php forms zend-framework2 validation


    【解决方案1】:

    你在正确的轨道上。您必须使用 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;
                        }
                    ),
                ),
            ),
        ),
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      相关资源
      最近更新 更多