Editing
Inserting a Hidden Date Field in a Symfony Form
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
[[Category:Symfony]] [[Category:Web Development]] == Overview == This demonstrates creating and using a data transformation class in Symfony forms.<ref>[http://stackoverflow.com/a/21092244 Symfony2, How to add a hidden date type field to a form?], Stackoverflow</ref> == DataTransformer class == <syntaxhighlight lang="php"> // src/YourBundle/Form/DataTransformer/DateTimeToStringTransformer 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; } } </syntaxhighlight> == Form type class for the new "hidden date" field type == <syntaxhighlight lang="php"> // src/YourBundle/Form/Type/HiddenDateTimeType.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( )); } } </syntaxhighlight> == Register the new field type as a service == <syntaxhighlight lang="yaml"> # app/config.services.yml # ... yourproject.hidden_datetime.form.type: class: YourProject\YourBundle\Form\Type\HiddenDateTimeType tags: - { name: form.type, alias: hidden_datetime } </syntaxhighlight> == Usage in forms == <syntaxhighlight lang="php"> public function buildForm(FormBuilder $builder, array $options) { $builder ->add('createdAt','hidden_datetime') ->add('comment','textarea'); } </syntaxhighlight> == Notes == <references />
Summary:
Please note that all contributions to Littledamien Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Littledamien Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information