ruwork/polyfill-form-dti-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.

Symfony Form polyfill bundle adding input=datetime_immutable to date types.

0.2.1 2018-02-11 11:04 UTC

This package is auto-updated.

Last update: 2023-07-12 04:31:08 UTC


README

GitHub license Travis branch Codecov branch Packagist

This package is a polyfill bundle 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.

Internally this bundle uses the ruwork/polyfill-form-dti package and plugs its type extensions and guesser into DI with all necessary tags.

Installation

composer require ruwork/polyfill-form-dti-bundle

Enable the Ruwork\PolyfillFormDTIBundle\RuworkPolyfillFormDTIBundle manually in your kernel if you don't use Symfony Flex.

Usage

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @Entity
 */
class Survey
{
    /**
     * @ORM\Column(type="date_immutable")
     *
     * @var \DateTimeImmutable
     */
    private $dayOfBirth;

    public function getDayOfBirth()
    {
        return $this->dayOfBirth;
    }

    public function setDayOfBirth(\DateTimeImmutable $dayOfBirth)
    {
        $this->dayOfBirth = $dayOfBirth;
    }
}
<?php

namespace App\Controller;

use App\Entity\Survey;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\HttpFoundation\Request;

class FormController extends AbstractController
{
    /**
     * @Route("/form")
     * @Template
     */
    public function __invoke(Request $request)
    {
        $survey = new Survey();

        $form = $this->createFormBuilder($survey)
            ->add('dayOfBirth', DateType::class, [
                'input' => 'datetime_immutable',
            ])
            // or let the form guesser do the job for you
            ->add('dayOfBirth')
            ->getForm()
            ->handleRequest($request);

        return [
            'form' => $form->createView(),
        ];
    }
}

Bundle will automatically register the Ruwork\PolyfillFormDTI\Guesser\DoctrineOrmDTIGuesser if Doctrine DBAL's version is >=2.6 and @doctrine service is registered in DI.

Testing

vendor/bin/simple-phpunit