我正在尝试以下方案:
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.
我知道如何隐藏日期类型字段!??
答案 0 :(得分:9)
表格
$builder
->add('day','hidden')
->add('date',null,array( 'attr'=>array('style'=>'display:none;')) )
...
答案 1 :(得分:8)
从DateTime对象到string创建一个简单的DataTransformer,并创建一个名为'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(
));
}
}
将新的“hidden_datetime”表单类型注册为services.yml
中的服务yourproject.hidden_datetime.form.type:
class: YourProject\YourBundle\Form\Type\HiddenDateTimeType
tags:
- { name: form.type, alias: hidden_datetime }
然后可以在表单中使用新的hidden_datetime类型:
// IN myclassType
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('createdAt','hidden_datetime')
->add('comment','textarea')
;
}
答案 2 :(得分: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
));
答案 3 :(得分:1)
表格
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>
答案 4 :(得分: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
答案 5 :(得分:1)
以前的解决方案都不适用于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
。