1

I want to set date format to all datetime and date fields used in app globally. I thought I can achieve that using form theme, but when I tried to pass format variable in twig nothing happened.

    {{ form_row(form.date,{'format': 'dd.MM.yy'}) }}

Any ideas? Thanks in advance.

2
  • @DarkBee thanks for your answer but it's not duplicate. I want to change datetime form field's format. To override options added in FormType. Commented Nov 10, 2017 at 9:58
  • Have you tried the global config, like: twig.date.format: dd.MM.yy? (reference: symfony.com/doc/current/reference/configuration/twig.html) Commented Nov 10, 2017 at 10:49

1 Answer 1

2

Finally found solution, which was pretty simple. I used Symfony's extensions to override core built-in DateType, so I could set my desired date format.

final class DateTypeExtension extends AbstractTypeExtension
{
 /**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
 $resolver->setDefaults(array(
  'widget' => 'single_text',
  'format' => 'dd.MM.yyyy',
));
}
   /**
   * {@inheritdoc}
   */
  public function getExtendedType()
 {
  return DateType::class;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.