barryvdh/laravel-form-bridge
Composer 安装命令:
composer require barryvdh/laravel-form-bridge
包简介
This packages integrates Symfony Form Component in Laravel.
关键字:
README 文档
README
See http://symfony.com/doc/current/forms.html
Laravel integration:
- Pre-set old input
- Add validation errors
- Translate field names
Install
composer require barryvdh/laravel-form-bridge- Add
Barryvdh\Form\ServiceProvider::class,to you ServiceProviders. - (optional) Add
'FormFactory' => Barryvdh\Form\Facade\FormFactory::class,to your Facades. - (optional) Add
'FormRenderer' => Barryvdh\Form\Facade\FormRenderer::class,to your Facades.
Basic example
You can use the FormFactory to create a form. You can supply a Model as data, so it will fill the values.
You can use $form->handleRequest($request); to update the values in the user, or you can just use $request object or Input facade like usual.
However, by default, the form is grouped under a form key, so you have to use $request->get('form') to get the form values.
Or you can create a Named form, with an empty name.
If you need to set more options, use the createBuilder function instead of create, to be able to use setAction() etc. You need to call ->getForm() to get the actual form instance again.
use FormFactory; use Illuminate\Http\Request; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; Route::any('create', function() { $user = App\User::first(); $form = FormFactory::create(FormType::class, $user) ->add('name', TextType::class) ->add('email', EmailType::class, [ 'rules' => 'unique:users,email', ]) ->add('save', SubmitType::class, ['label' => 'Save user']); $form->handleRequest(); if ($form->isSubmitted() && $form->isValid()) { // Save the user with the new mapped data $user->save(); return redirect('home')->withStatus('User saved!'); } return view('user.create', compact('form')); });
Use the following in your Blade templates:
@formStart($form) @formWidget($form) @formEnd($form)
Other directives are: @form, @formLabel, @formErrors, @formRest and @formRow
@form($form)
@formStart($form) <h2>Name</h2> @formLabel($form['name'], 'Your name') @formWidget($form['name'], ['attr' => ['class' => 'name-input']]) <h2>Rest</h2> @formRest($form) @formEnd($form)
Or use the following in your Twig templates to render the view:
{{ formStart(form) }}
{{ formWidget(form) }}
{{ formEnd(form) }}
See http://symfony.com/doc/current/book/forms.html#form-rendering-template for more options.
Traits
To make it easier to use in a Controller, you can use 2 traits:
ValidatesForms: Adds a validation method, similar to the ValidatesRequests trait:
$this->validateForm($form, $request, $rules)
CreatesForms: Create a Form or FormBuilder:
- createForm($type, $data, $options) -> Form for a type (
formor a Type class) - createNamed($name, $type, $data, $options) -> Form with a given name
- createFormBuilder($data, $options) -> FormBuilder with an empty name
- createNamedFormBuilder($name, $data, $options) -> FormBuilder with a given name
use Barryvdh\Form\ValidatesForms; use Barryvdh\Form\CreatesForms; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; class UserController extends Controller{ use ValidatesForms, CreatesForms; public function anyIndex(Request $request) { $user = User::first(); $form = $this->createFormBuilder($user) ->add('name', TextType::class) ->add('email', EmailType::class) ->add('save', SubmitType::class, ['label' => 'Save user']) ->getForm(); $form->handleRequest($request); if ($form->isSubmitted()) { $this->validateForm($form, $request, [ 'name' => 'required', 'email' => 'required|email', ]); $user->save(); } return view('user', ['form' => $form->createView()]); } }
Creating a named form:
use Symfony\Component\Form\Extension\Core\Type\FormType; $form = $this->createNamed('user', FormType::class, $user) ->add('name', TextType::class) ->add('email', EmailType::class) ->add('save', SubmitType::class, ['label' => 'Save user']);
See http://symfony.com/doc/current/book/forms.html for more information.
BelongsToMany relations
BelongsToMany behaves differently, because it isn't an actual attribute on your model. Instead, we can set the mapped option to false and sync it manually.
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; $builder->add('users', ChoiceType::class, [ 'choices' => \App\User::pluck('name', 'id'), 'multiple' => true, 'mapped' => false, 'expanded' => true, // true=checkboxes, false=multi select ]);
$form->handleRequest($request); if ($form->isSubmitted()) { $this->validate($request, $rules); $item->save(); $item->users()->sync($form->get('users')->getData()); return redirect()->back(); }
See for more options the choice type documentation.
Note: The BelongsToManyType is deprecated in favor of the ChoiceType from Symfony.
Translation labels
If you want to translate your labels automatically, just pass the translation key as the label attribute. It will run throught Twig's trans filter.
->add('name', TextType::class, ['label' => 'fields.name'])
Uploading Files
You can use the file type in the FormBuilder, and use the magic getFile() and setFile() method on your Model or mark it as not mapped, so you can handle it yourself. See http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
Class User extends Model { /** @var UploadedFile */ private $file; public function getFile() { return $this->file; } public function setFile(UploadedFile $file = null) { $this->file = $file; } public function upload() { // the file property can be empty if the field is not required if (null === $this->getFile()) { return; } // use the original file name here but you should // sanitize it at least to avoid any security issues // move takes the target directory and then the // target filename to move to $this->getFile()->move( $this->getUploadRootDir(), $this->getFile()->getClientOriginalName() ); // set the path property to the filename where you've saved the file $this->path = $this->getFile()->getClientOriginalName(); // clean up the file property as you won't need it anymore $this->file = null; } }
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; $user = User::first(); $form = $this->createFormBuilder($user) ->add('name', TextType::class) ->add('file', FileType::class) ->add('save', SubmitType::class, ['label' => 'Save user']) ->getForm(); $form->handleRequest($request); if ($form->isValid()) { $user->upload(); $user->save(); }
Extending
You can extend some of the arrays in the ServiceProvider, eg. to add Types, add this to the register() method in your own ServiceProvider:
$this->app->extend('form.types', function($types, $app){ $types[] = new CustomType(); return $types; });
barryvdh/laravel-form-bridge 适用场景与选型建议
barryvdh/laravel-form-bridge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 370.63k 次下载、GitHub Stars 达 163, 最近一次更新时间为 2015 年 04 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「form」 「Forms」 「laravel」 「formbuilder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 barryvdh/laravel-form-bridge 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 barryvdh/laravel-form-bridge 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 barryvdh/laravel-form-bridge 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
ConfirmationField is a form field for Atk14 applications. It's like the BooleanField (checkbox) but the ConfirmationField must be ticked.
Field for number with restricted count of digits and decimal places
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
HTML and form generation
Build forms from schema
A Laravel Filament Forms slug field.
统计信息
- 总下载量: 370.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 168
- 点击次数: 22
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-04-20