7

我正在尝试以下场景:

myclassType

public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('day','hidden')
        ->add('date', 'hidden' ) 
        ->add('hours')
        ->add('comment','textarea')
    ;
}

myclass

class myclass
{
    //.. Other stuff

    /**
     * @ORM\Column(type="date")
     *
     * @var date $date
     */
    protected $date;
}

渲染时出现此错误:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error:
 Object of class DateTime could not be converted to string in 
C:\wamp\www\PMI_sf2\app\cache\dev\twig\fb\40\8957f80f2358a6f4112c3427b387.php line 684") in
 form_div_layout.html.twig at line 171.

知道如何隐藏日期类型字段!??

4

6 回答 6

11

Form

$builder
    ->add('day','hidden')
    ->add('date',null,array( 'attr'=>array('style'=>'display:none;')) )
... 
于 2012-05-22T20:59:20.037 回答
8

创建一个从 DateTime 对象到字符串的简单 DataTransformer 和一个名为 ie 'hidden_​​datetime' 的新表单类型,它使用新的 DataTransformer 并将隐藏的表单类型作为父级。

<?php

namespace YourProject\YourBundle\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;

class DateTimeToStringTransformer implements DataTransformerInterface
{

    public function __construct()
    {   
    }

    /**
     * @param \DateTime|null $datetime
     * @return string
     */
    public function transform($datetime)
    {
        if (null === $datetime) {
            return '';
        }       
        return $datetime->format('Y-m-d H:i:s');
    }

    /**
     * @param  string $datetimeString
     * @return \DateTime
     */
    public function reverseTransform($datetimeString)
    {
        $datetime = new \DateTime($datetimeString);
        return $datetime;
    }
} 

..

<?php

namespace YourProject\YourBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
use YourProject\YourBundle\Form\DataTransformer\DateTimeToStringTransformer;

class HiddenDateTimeType extends AbstractType
{

    public function __construct()
    {
    }

    public function getName()
    {
        return 'hidden_datetime';
    }

    public function getParent()
    {
        return 'hidden';
    }   

     public function buildForm(FormBuilderInterface $builder, array $options)
    {   
        $transformer = new DateTimeToStringTransformer();
        $builder->addModelTransformer($transformer);
    }   

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        parent::setDefaultOptions($resolver);   
        $resolver->setDefaults(array(
        ));
    }    
}

在 services.yml 中将新的“hidden_​​datetime”表单类型注册为服务

yourproject.hidden_datetime.form.type:
    class: YourProject\YourBundle\Form\Type\HiddenDateTimeType
    tags:
        - { name: form.type, alias: hidden_datetime } 

然后可以在您的表单中使用新的 hidden_​​datetime 类型:

// 在我的类类型中

public function buildForm(FormBuilder $builder, array $options)
{
    $builder
            ->add('createdAt','hidden_datetime')
            ->add('comment','textarea')
    ;
}
于 2014-01-13T13:14:32.803 回答
4

以前的解决方案都不适用于 Symfony 3.3。我最终创建了一个简单的类型:

<?php

namespace <YourProject>\<YourBundle>\Form\Type;

use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

class HiddenDateTimeType extends HiddenType implements DataTransformerInterface
{
    /**
    * {@inheritdoc}
    */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addModelTransformer($this);
    }

    /**
    * {@inheritdoc}
    */
    public function transform($data)
    {
        return $data->format("Y-m-d H:i:s");
    }

    /**
    * {@inheritdoc}
    */
    public function reverseTransform($data)
    {
        try {
            return new \DateTime($data);
        } catch (\Exception $e) {
            throw new TransformationFailedException($e->getMessage());
        }
    }

    public function getName()
    {
        return 'hidden_datetime';
    }
}

在您的表单中使用它,如下所示:

<?php

namespace <YourProject>\<YourBundle>\Form;

use <YourProject>\<YourBundle>\Form\Type\HiddenDateTimeType;

class ReportType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('creationTime', HiddenDateTimeType::class, array(
                'required' => true,
            ));
    }
}

没有什么可添加的services.yml

于 2018-02-06T09:29:41.750 回答
3

更容易。基于martti解决方案

只需扩展 DateTimeType 并将父级定义为“隐藏”。

<?php

namespace YourProject\YourBundle\Form\Type\HiddenDateTimeType

use Symfony\Component\Form\Extension\Core\Type\DateTimeType;

class HiddenDateTimeType extends DateTimeType
{
    public function getName()
    {
        return 'hidden_datetime';
    }

    public function getParent(array $options)
    {
        return 'hidden';
    }
}

将表单类型注册为服务:

yourproject.hidden_datetime.form.type:
    class: YourProject\YourBundle\Form\Type\HiddenDateTimeType
    tags:
        - { name: form.type, alias: hidden_datetime }

您将可以完全访问所有特定的 DateTimeType 选项

public function buildForm(FormBuilder $builder, array $options)
{

    $builder
        ->add('from' , 'hidden_date', array(
            'widget' => 'single_text',
            'format' => 'Y-m-d',
            'label' => 'popin.from',
            'required' => true
        ));
于 2014-02-11T10:12:32.087 回答
1

您可以比这更直接地控制表单输出。主题化特定领域非常容易。有两个步骤。您需要确保表单字段期望日期的单个字符串表示。所以将日期小部件设置为single_text. 然后覆盖日期字段以输出隐藏字段而不是文本字段。

形式

$builder->add('date', 'date', array('widget' => 'single_text'));

枝条

{% form_theme form _self %}

{# Makes all dates hidden, you can also name your field specifically #}
{% block date_row %}
    {{ block('hidden_widget') }} {# This is normally 'field_widget' #}
{% endblock %}

请参阅http://symfony.com/doc/current/cookbook/form/form_customization.html#form-theming

于 2013-11-25T09:55:46.510 回答
0

形式

public function buildForm(FormBuilder $builder, array $options){
    $builder
        ->add('day','hidden')
        ->add('date') 
        ->add('hours')
        ->add('comment','textarea');
}

看法:

<form action="{{ path('some-save-action') }}" method="post" {{ form_enctype(form) }}>
    <div style="display: none">
         {{ form_row(form.date) }}
    </div>

    {{ form_errors(form) }}
    {{ form_rest(form) }}
    <input type="submit" value="Save" class="button confirm big"/>
</form>
于 2012-05-22T20:56:09.710 回答