如何在 Silex/Twig 中注册自定义表单字段类型? [英] How can I register a custom form field type in Silex / Twig?

查看:31
本文介绍了如何在 Silex/Twig 中注册自定义表单字段类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在运行,但是当我使用自定义 Form-Field-Type (shtumi_daterange) 时:

My App ist running, but when I use a custom Form-Field-Type (shtumi_daterange) like:

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

我想,我需要这样的东西:

I think, I need something like:

<?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',
));

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

but I just dont know/find the right symtax!

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

From the symfony documentation, I found this example how to register a custom form field type, but I can't transform this to get it run with silex:

services:

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

推荐答案

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

You do not need to register it to be able to use it. You can pass a new instance as the second parameter of 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));
}

它在 您链接的文档中提到.

这篇关于如何在 Silex/Twig 中注册自定义表单字段类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆