ruwork/polyfill-form-dti-bundle

此包已被弃用,不再维护。未建议替代包。

Symfony Form polyfill bundle,为日期类型添加 input=datetime_immutable。

安装量: 46,747

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 6

分支: 0

开放问题: 0

类型:symfony-bundle

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

此包是我为添加 input=datetime_immutable 选项到 Symfony 日期和时间表单类型的 pull request 创建的 polyfill bundle。

适用于 PHP >=5.5 和 Symfony >=2.8

内部此包使用 ruwork/polyfill-form-dti 包,并将其类型扩展和猜测器通过所有必要的标签注入到 DI 中。

安装

composer require ruwork/polyfill-form-dti-bundle

如果您不使用 Symfony Flex,请手动在您的内核中启用 Ruwork\PolyfillFormDTIBundle\RuworkPolyfillFormDTIBundle

使用

<?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(),
        ];
    }
}

如果 Doctrine DBAL 的版本为 >=2.6 并且已注册 @doctrine 服务,则包将自动注册 Ruwork\PolyfillFormDTI\Guesser\DoctrineOrmDTIGuesser

测试

vendor/bin/simple-phpunit