[英]How to add a custom hydrator strategy for doctrine Date hydrator
I have a form which contains 3 field of type text and with a Date validator. 我有一个包含3个文本类型的字段和一个日期验证器的表单。
This code is in a fieldset using doctrine hydrator (related to a doctrine entity) 此代码在使用准则水化器的字段集中(与准则实体有关)
$this->add(
array(
'name' => 'endDate',
'type' => 'Zend\Form\Element\Text',
'options' => array(
'label' => 'end_date_label',
'label_attributes' => array(
'class' => 'control-label col-xs-3'
),
),
'attributes' => array(
'class' => 'form-control col-xs-3 datepicker-end-date',
)
)
);
'endDate' => array(
'required' => true,
'allow_empty' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'Date',
'options' => array(
'format' => 'd/m/Y',
),
),
),
),
I working with french date format. 我使用法语日期格式。 When i valid the form if i change the format to m/d/Y it's working but when my form is not valid, my date picker get the wrong date (month and days are inversed).
当我将表格更改为m / d / Y时,如果表格有效,则可以使用,但是当表格无效时,日期选择器会得到错误的日期(月份和日期是相反的)。
What i want is to valid a french date format, and save into Database a date in m/d/Y format. 我想要的是有效的法语日期格式,并将m / d / Y格式的日期保存到数据库中。
With this format i get the error : 用这种格式我得到错误:
DateTime::__construct(): Failed to parse time string (29/04/2015) at position 0 (2): Unexpected character
I saw many post on Stack talking about custom strategy for doctrine hydration but i didn't understand them. 我在Stack上看到许多帖子,都在谈论关于理论水合的自定义策略,但我不理解它们。 What i'm supposed to do step by step ?
我应该一步一步做什么?
I tried to add a strategy for my field endDate but it's never called...This code is in the fieldset class just before my fields declaration : 我试图为我的字段endDate添加策略,但从未调用过……此代码在我的字段声明之前的fieldset类中:
$this->setHydrator(new DoctrineHydrator($this->getObjectManager(), 'TodoList\Entity\TodoQuestion'))
->setObject(new TodoQuestion());
$this->getHydrator()->addStrategy('endDate', new \Application\Strategy\DateTimeStrategy());
And my Datetime strategy implements strategy interface. 我的Datetime策略实现了策略界面。
<?php
namespace Application\Strategy;
use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;
class DateTimeStrategy implements StrategyInterface
{
public function hydrate($value)
{
if (is_string($value)) {
$value = new DateTime($value);
}
return $value->format('d/m/Y');
}
public function extract($value)
{
return;
}
}
If someone can explain with details what i'm doing wrong and help me to understand this whole thing.. 如果有人可以详细解释我在做什么错,并帮助我理解整个过程。
You should just return a DateTime object from the Strategy.
您应该只从策略中返回一个DateTime对象。
namespace Application\\Strategy; use Zend\\Stdlib\\Hydrator\\Strategy\\StrategyInterface; class DateTimeStrategy implements StrategyInterface { public function hydrate($value) { if (is_string($value)) { $value = \\DateTime::createFormFormat('d/m/Y', $value); } return $value; } public function extract($value) { return $value; } }
Above isn't going to work since strategies are called after the hydrator's type conversion. 上面的方法不起作用,因为策略是在水化器的类型转换之后调用的。
You're best of with using a Callback filter. 最好使用回调过滤器。
'endDate' => array(
'required' => true,
'allow_empty' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
array(
'name' => 'Callback',
'options' => array(
'callback' => function($value) {
if (is_string($value)) {
$value = \DateTime::createFromFormat('d/m/Y', $value);
}
return $value;
},
),
),
'validators' => array(
array(
'name' => 'Date',
'options' => array(
'format' => 'd/m/Y',
),
),
),
),
The doctrine hydrator you are using seems outdated btw. 您正在使用的理论式水化器似乎过时了。 Current version doesn't require to specify the entity as a second parameter.
当前版本不需要将实体指定为第二个参数。
Stumbled onto this question by accident. 偶然发现了这个问题。 Though it's reasonably old, I was recently working with Dates in ZF2 forms.
尽管它已经相当老了,但是我最近正在使用ZF2格式的Dates。 I've got my formatting done as below, without using a callback.
我已经完成了下面的格式化,没有使用回调。
Maybe it'll help someone in the future ;) 也许将来会对某人有所帮助;)
Below was done using ZF2 2.5.3 以下是使用ZF2 2.5.3完成的
$this->add([
'name' => 'startDate',
'required' => true,
'filters' => [
[
'name' => DateTimeFormatter::class,
'options' => [
'format' => 'Y-m-d', // or d/m/Y
],
],
],
'validators' => [
[
'name' => Date::class,
'options' => [
'format' => 'Y-m-d', // or d/m/Y
],
],
],
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.