0

我的应用程序正在运行,但是当我使用自定义表单字段类型(shtumi_daterange)时,例如:

public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options)
{
    $dateRange4 = new DateRange('m/d/Y');
    $dateRange4->parseData('03/27/2012 - 04/05/2012');
    $builder->add('builddate', "shtumi_daterange", array('required'=>false, 'default'=>$dateRange4));
}

发生此错误:

Uncaught exception 'Symfony\Component\Form\Exception\FormException' with 
message 'Could not load type "shtumi_daterange"' in 
../vendor/symfony/form/Symfony/Component/Form/FormRegistry.php:95

我想,我需要类似的东西:

<?php
$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path'        => $path_ext_dir_abs . '/views',
'twig.form.templates' => array('form_div_layout_custom.html.twig'),
    'form.type.shtumi_daterange' => 'Shtumi\UsefulBundle\Form\Type\DateRangeType',
));

但我只是不知道/找到正确的符号!

symfony 文档中,我找到了如何注册自定义表单字段类型的示例,但我无法对其进行转换以使其与 silex 一起运行:

services:

  form.type.gender:
      class: Acme\DemoBundle\Form\Type\GenderType
      arguments:
          - "%genders%"
      tags:
          - { name: form.type, alias: gender }
4

2 回答 2

3

我通过扩展FormServiceProvider.

use Symfony\Component\Form\FormFactoryInterface;

$app->extend('form.factory', function (FormFactoryInterface $factory) {
    $factory->addType(new MyType());

    return $factory;
});
于 2012-09-27T10:27:20.340 回答
1

您无需注册即可使用它。您可以传递一个新实例作为第二个参数add

use Shtumi\UsefulBundle\Form\Type\DateRangeType;
/* ... */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $dateRange4 = new DateRange('m/d/Y');
    $dateRange4->parseData('03/27/2012 - 04/05/2012');
    $builder->add('builddate', new DateRangeType(), array('required'=>false, 'default'=>$dateRange4));
}

您链接的文档中提到了它。

于 2012-08-11T15:10:55.507 回答