2012-05-22 70 views
3

我想下面的場景:Symfony2,如何將隱藏日期類型字段添加到表單?

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

出於興趣,爲什麼要隱藏呢?什麼是用例? – Flukey

回答

8

形式

$builder 
    ->add('day','hidden') 
    ->add('date',null,array('attr'=>array('style'=>'display:none;'))) 
... 
+7

注: 您還可以擴展默認的Symfony字段類型添加選項隱藏和擴展型塊「form_row」,添加樣式=「無顯示」:有些瀏覽器(例如,IE8)獲如果設置了「display:none」,則不提交該字段。改爲使用'visibility:hidden'。 –

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> 
+0

我喜歡這個主意,但是在Symfony2中對此有任何支持嗎? – PMoubed

+0

不要這麼想...... @ a.aitboudad有其他解決方案,但是如果包含DIV的樣式,它仍然可見。在包含DIV - 如果你使用DIV佈局 –

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

+0

這仍然適用於Symfony 3.4,但您需要重寫類型。 {% - set type ='hidden' - %} –

8

創建從DateTime對象的簡單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') 
    ; 
} 
+3

已經存在一個用於此目的的DateTimeTransformer:'Symfony \ Component \ Form \ Extension \ Core \ DataTransformer \ DateTimeToStringTransformer' –

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 
     )); 
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

+0

代碼中的輕微錯字應該是: '拋出新TransformationFailedException($ E->的getMessage());' – navitronic

+1

@navitronic感謝報告這個錯誤。這是在實際的代碼,並沒有被注意到:-) –

相關問題