ruwork/polyfill-form-dti

Symfony Form polyfill adding input=datetime_immutable to date types.

Installs: 48 409

Dependents: 1

Suggesters: 0

Security: 0

Stars: 1

Watchers: 5

Forks: 0

Open Issues: 0

Type:symfony-bundle

0.2.1 2018-02-16 15:21 UTC

This package is auto-updated.

Last update: 2024-11-12 07:28:02 UTC


README

GitHub license Travis branch Codecov branch Packagist

This package is a polyfill for my pull request adding input=datetime_immutable option to the Symfony date and time form types.

Works with PHP >=5.5 and Symfony >=2.8.

If you use Symfony Framework have a look at the corresponding ruwork/polyfill-form-dti-bundle.

Installation

composer require ruwork/polyfill-form-dti

Usage

<?php

use Ruwork\PolyfillFormDTI\DTIExtension;
use Ruwork\PolyfillFormDTI\Guesser\DoctrineOrmDTIGuesser;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TimeType;
use Symfony\Component\Form\Forms;

/** @var \Doctrine\Common\Persistence\ManagerRegistry $registry */

$factory = Forms::createFormFactoryBuilder()
    ->addExtension(new DTIExtension())
    // Optionally you can add a Doctrine ORM guesser
    // to guess input=datetime_immutable for Doctrine 2.6 immutable date types. 
    ->addTypeGuesser(new DoctrineOrmDTIGuesser($registry))
    ->getFormFactory();

$form = $factory
    ->createBuilder(FormType::class, [
        'datetime' => new \DateTimeImmutable('1828-09-09 12:00:00'),
        'date' => new \DateTimeImmutable('1860-01-29'),
        'time' => new \DateTimeImmutable('23:59'),
    ])
    ->add('datetime', DateTimeType::class, [
        'input' => 'datetime_immutable',
    ])
    ->add('date', DateType::class, [
        'input' => 'datetime_immutable',
    ])
    ->add('time', TimeType::class, [
        'input' => 'datetime_immutable',
    ])
    ->getForm();

Testing

vendor/bin/simple-phpunit