ansien / rapid-form-bundle
一个帮助你快速为你的 Symfony 应用程序创建表单的套餐
1.1.7
2024-03-14 18:22 UTC
Requires
- php: >=8.0
- symfony/config: ^5.2|^6.0|^7.0
- symfony/form: ^5.2|^6.0|^7.0
- symfony/framework-bundle: ^5.2|^6.0|^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.18
- phpro/grumphp: ^1.3
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^5.2|^6.0|^7.0
README
该套餐的目标是使用 PHP 8 属性 在你的 DTO 上构建 Symfony 表单。
问题
在 Symfony 中制作表单相对简单。但是一旦你开始使用 DTO,你将不得不维护两个类:你的 DTO 和你的 Symfony 表单类型。这并不理想,因为它会产生不必要的劳动、维护,并且很容易导致错误。
解决方案
此套餐将显著加快你在 Symfony 应用程序中创建表单的速度。通过提供的 PHP 8 属性,你可以通过装饰你的 DTO 快速构建表单,不再需要维护两个不同的类。
安装
您可以通过 Composer 安装此包
composer require ansien/rapid-form-bundle
用法
表单
<?php declare(strict_types=1); namespace App\Form; use Ansien\RapidFormBundle\Attribute\Form; use Ansien\RapidFormBundle\Attribute\FormField; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Validator\Constraints as Assert; #[Form] class ExampleForm { #[FormField(TextType::class, [ 'required' => true, ])] #[Assert\NotBlank] public ?string $name = null; #[FormField(TextType::class)] public ?string $description = null; }
控制器
<?php declare(strict_types=1); namespace App\Controller; use Ansien\RapidFormBundle\Form\RapidFormBuilderInterface; use App\Form\ExampleForm; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class ExampleController extends AbstractController { public function __construct(private RapidFormBuilderInterface $formBuilder) { } #[Route('/example', methods: ['GET', 'POST'])] public function __invoke(Request $request): Response { $data = new ExampleForm(); $form = $this->formBuilder->create($data)->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { // Do something with $data } return $this->render('example.html.twig', [ 'form' => $form->createView(), ]); } }
有关更多示例,请参阅 ./examples
。
变更日志
请参阅 变更日志 了解最近更改的详细信息。
贡献
请参阅 贡献指南 了解详细信息。
支持者
鸣谢
- Andries
- Albert(为其对已弃用的 annotated-form-bundle 的工作做出贡献)
- schvoy(为其在 form-annotation-bundle 上的工作做出贡献)
- Bob 和 Jon(为其存储库模板做出贡献)
- 所有贡献者
许可证
MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。