Zend framework2 在Zend Framework 2中,如何更改DateStep验证程序中的消息
在\Zend\Validator\DateStep中,我想覆盖如下所示的错误消息:Zend framework2 在Zend Framework 2中,如何更改DateStep验证程序中的消息,zend-framework2,Zend Framework2,在\Zend\Validator\DateStep中,我想覆盖如下所示的错误消息: protected $messageTemplates = array( self::NOT_STEP => "The input is not a valid step" ); 我有一个输入过滤器连接到一个表单,该表单包含一个类型为“Zend\form\element\Date”的元素,该元素会自动调用DateStep验证器 $this->add(array( 'type
protected $messageTemplates = array(
self::NOT_STEP => "The input is not a valid step"
);
我有一个输入过滤器连接到一个表单,该表单包含一个类型为“Zend\form\element\Date”的元素,该元素会自动调用DateStep验证器
$this->add(array(
'type' => 'Zend\Form\Element\Date',
'name' => 'appointment-date',
'options' => array(
'label' => 'Appointment Date',
'format' => 'Y-m-d'
),
'attributes' => array(
'min' => date('Y-m-d'), // today's date
'max' => '2020-01-01',
'step' => '2', // days; default step interval is 1 day
)
));
以下是我表格的相关部分:
$this->add(array(
'type' => 'Zend\Form\Element\Date',
'name' => 'appointment-date',
'options' => array(
'label' => 'Appointment Date',
'format' => 'Y-m-d'
),
'attributes' => array(
'min' => date('Y-m-d'), // today's date
'max' => '2020-01-01',
'step' => '2', // days; default step interval is 1 day
)
));
这是我的输入过滤器:
$inputFilter->add($factory->createInput(array(
'name' => 'appointment-date',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'DateStep',
'options' => array(
//'step' => new DateInterval("P2D"),
//'baseValue' => new DateTime(),
'messages' => array(
\Zend\Validator\DateStep::NOT_STEP => 'Must be a day in the future',
),
),
),
),
)));
inputFilter似乎被忽略了。我尝试在inputFilter中设置步长和baseValue,但这似乎根本不起作用。如果需要更多详细信息,可以在此处找到工作应用程序。看起来您正在尝试使用“DateStep”验证器过滤“Date”输入
$this->add(array(
'type' => 'Zend\Form\Element\Date',
'name' => 'appointment-date',
'options' => array(
'label' => 'Appointment Date',
'format' => 'Y-m-d'
),
'attributes' => array(
'min' => date('Y-m-d'), // today's date
'max' => '2020-01-01',
'step' => '2', // days; default step interval is 1 day
)
));
这可能会导致您的问题。感谢您对此进行调查。无论是否添加输入筛选器,类型为“Zend\Form\Element\Date”的元素都会自动调用DateStep。我尝试添加一个messages属性,以便将min、max、step和messages放入构造函数,但这也不起作用。请查看Zend\Form\Element\Date的方法
getStepValidator()
。它似乎在构建自己的验证器。我建议您在表单构造函数中添加这一行:$this->setPreferFormInputFilter(true)代码>这将告诉你的程序更喜欢你的inputfilter而不是默认的;这就是诀窍。以防其他任何人试图复制此信息;完成的输入筛选器如下所示:$inputFilter->add($factory->createInput(数组(…'validators'=>array('name'=>'DateStep','options'=>array('step'=>new DateInterval(“P2D”),'baseValue'=>new DateTime(),'format'=>Y-m-d','messages'=>array(\Zend\Validator\DateStep::NOT_STEP=>'必须是未来的一天',),),,),),),),);