我需要一个自定义字段,它接受字符串或DateTime对象作为输入。经过一番讨论后,我决定使用DataTransformers。我按照Cookbook上的说明操作,但我收到了以下错误:
捕获致命错误:参数1传递给 Symfony \ Component \ Form \ FormFactory :: loadTypeExtensions()必须 实现接口Symfony \ Component \ Form \ FormTypeInterface,实例 Yanic \ HomeBundle \ Form \ DataTransformer \ DateToStringTransformer 给予,召唤 第320行的/Applications/MAMP/htdocs/symfony-standard-2.1/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php并在 /Applications/MAMP/htdocs/symfony-standard-2.1/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 332
似乎期待FormType,但文档说这不应该是必要的。
这就是我正在使用的代码:
UsersType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new \Yanic\HomeBundle\Form\DataTransformer\DateToStringTransformer();
$builder
->add('username')
->add('password','password',
array(
'required' => false
))
->add('email')
->addViewTransformer( $transformer )
->add('created', 'datetimeToString', array(
'disabled' => true,
))
->add('modified', 'datetimeToString', array(
'disabled' => true,
))
->add('isActive', null, array(
'label' => 'Is active?',
'required' => false
))
->add('lastLogin', null, array(
'empty_data' => 'never',
'widget' => 'single_text',
'read_only' => true,
'label' => 'Last login'
))
->add('modifiedBy', null, array(
'label' => 'Modified by',
'read_only' => true
))
->add('groups')
;
}
DateToStringTransformer.php
namespace Yanic\HomeBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class DateToStringTransformer implements DataTransformerInterface
{
/**
* Transforms an DateTime object to a string.
*
* @param \DateTime|null $issue
* @return string
*/
public function transform($date)
{
if (null === $date) {
return "never";
}
return $date->format('d-m-Y H:i');
}
/**
* Transforms a string (date formatted) to an object (\DateTime).
*
* @param string $date
* @return \DateTime|null
* @throws TransformationFailedException
*/
public function reverseTransform($number)
{
return $number;
}
}
services.yml
services:
form.dataTransformer.datetimeToString:
class: Yanic\HomeBundle\Form\DataTransformer\DateToStringTransformer
tags:
- { name: form.type, alias: datetimeToString }
我做错了什么?
啊,我正在使用Symfony 2.1 Beta2
答案 0 :(得分:0)
在documentation中提示:
<?php
use Symfony\Component\Form\FormBuilderInterface;
use Acme\TaskBundle\Form\DataTransformer\IssueToNumberTransformer;
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
// this assumes that the entity manager was passed in as an option
$entityManager = $options['em'];
$transformer = new IssueToNumberTransformer($entityManager);
// use a normal text field, but transform the text into an issue object
$builder
->add('issue', 'text')
->addViewTransformer($transformer)
;
}
// ...
}
“使用普通文本字段,但将文本转换为问题对象”**
您已将数据转换器注册为服务中的表单类型。 Symfony将数据转换器视为表单类型。
您应该创建表单类型并将数据转换器放在那里(如在doc中),或删除服务定义,使用普通日期字段而不是datetimeToString。
但我认为在这种情况下,更好的将是第一种方式。