Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Latest commit

 

History

History
99 lines (76 loc) · 3.02 KB

README.md

File metadata and controls

99 lines (76 loc) · 3.02 KB

Symfony Form DateTimeImmutable Polyfill Bundle

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