我正在渲染一个表单以向数据库添加一个类(课程)。该类有一定的开始时间和结束时间。两者都是时间字段。我为该类创建了一个字段集:
<?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,但不确定。