A set of Blade components to rapidly build forms with Tailwind CSS v1, Tailwind CSS v2, Bootstrap 4 and Bootstrap 5. Supports validation, model binding, default values, translations, includes default vendor styling and fully customizable!
Hey! We've built a Docker-based deployment tool to launch apps and sites fully containerized. You can find all features and the roadmap on our website, and we are on Twitter as well!
Looking for Inertia/Vue.js support? Check out Form Components Pro
We proudly support the community by developing Laravel packages and giving them away for free. Keeping track of issues and pull requests takes time, but we're happy to help! If this package saves you time or if you're relying on it professionally, please consider supporting the maintenance and development.
You can install the package via composer:
composer require protonemedia/laravel-form-components
If you're using Tailwind, make sure the right plugin (v1 or v2) is installed and configured.
<x-form>
@bind($user)
<x-form-input name="last_name" label="Last Name" />
<x-form-select name="country_code" :options="$options" />
<x-form-select name="interests[]" :options="$multiOptions" label="Select your interests" multiple />
<!-- \Spatie\Translatable\HasTranslations -->
<x-form-textarea name="biography" language="nl" placeholder="Dutch Biography" />
<x-form-textarea name="biography" language="en" placeholder="English Biography" />
<!-- Inline radio inputs -->
<x-form-group name="newsletter_frequency" label="Newsletter frequency" inline>
<x-form-radio name="newsletter_frequency" value="daily" label="Daily" />
<x-form-radio name="newsletter_frequency" value="weekly" label="Weekly" />
</x-form-group>
<x-form-group>
<x-form-checkbox name="subscribe_to_newsletter" label="Subscribe to newsletter" />
<x-form-checkbox name="agree_terms" label="Agree with terms" />
</x-form-group>
<x-form-submit />
@endbind
</x-form>
Generating HTML in PHP is always quite opinionated and limited. Blade Components are great because additional attributes are passed down to the element. That's why we prefer writing forms using components instead of using PHP builders. This way, you don't have to write extensions or custom code for any attribute you pass in. Let's take a look at this x-form
example.
The action
attribute is optional, but you can pass a hard-coded, primitive value to the component using a simple HTML attribute. PHP expressions and variables can be passed to the component as well via attributes that use the :
character as a prefix. Do you need Alpine.js or VueJS directives? No problem!
<x-form action="/api/user">
<!-- ... -->
</x-form>
<x-form :action="route('api.user.store')" v-on:submit="checkForm">
<!-- ... -->
</x-form>
You can switch frameworks by updating the framework
setting in the form-components.php
configuration file. Use the artisan vendor:publish
command to publish the configuration file.
return [
'framework' => 'bootstrap-4',
];
No further configuration is needed unless you want to customize the Blade views and components.
The minimum requirement for an input
or textarea
is the name
attribute.
<x-form-input name="company_name" />
Optionally you can add a label
attribute, which can be computed as well.
<x-form-input name="company_name" label="Company name" />
<x-form-input name="company_name" :label="trans('forms.company_name')" />
You can also choose to use a placeholder
instead of a label, and of course you can change the type
of the element.
<x-form-input type="email" name="current_email" placeholder="Current email address" />
By default, every element shows validation errors, but you can hide them if you want.
<x-form-textarea name="description" :show-errors="false" />
You can use the default
attribute to specify the default value of the element.
<x-form-textarea name="motivation" default="I want to use this package because..." />
Instead of setting a default value, you can also pass in a target, like an Eloquent model. Now the component will get the value from the target by the name
.
<x-form-textarea name="description" :bind="$video" />
In the example above, where $video
is an Eloquent model, the default value will be $video->description
.
You can also bind a target by using the @bind
directive. This will bind the target to all elements until the @endbind
directive.
<x-form>
@bind($video)
<x-form-input name="title" label="Title" />
<x-form-textarea name="description" label="Description" />
@endbind
</x-form>
You can even mix targets!
<x-form>
@bind($user)
<x-form-input name="full_name" label="Full name" />
@bind($userProfile)
<x-form-textarea name="biography" label="Biography" />
@endbind
<x-form-input name="email" label="Email address" />
@endbind
</x-form>
You can override the @bind
directive by passing a target directly to the element using the :bind
attribute. If you want to remove a binding for a specific element, pass in false
.
<x-form>
@bind($video)
<x-form-input name="title" label="Title" />
<x-form-input :bind="$videoDetails" name="subtitle" label="Subtitle" />
<x-form-textarea :bind="false" name="description" label="Description" />
@endbind
</x-form>
You can use the @wire
and @endwire
directives to bind a form to a Livewire component. Let's take a look at the ContactForm
example from the official Livewire documentation.
use Livewire\Component;
class ContactForm extends Component
{
public $name;
public $email;
public function submit()
{
$this->validate([
'name' => 'required|min:6',
'email' => 'required|email',
]);
Contact::create([
'name' => $this->name,
'email' => $this->email,
]);
}
public function render()
{
return view('livewire.contact-form');
}
}
Normally you would use a wire:model
attribute to bind a component property with a form element. By using the @wire
directive, this package will automatically use the wire:model
attribute instead of the name
attribute.
<x-form wire:submit.prevent="submit">
@wire
<x-form-input name="name" />
<x-form-input name="email" />
@endwire
<x-form-submit>Save Contact</x-form-submit>
</form>
Additionally, you can pass an optional modifier to the @wire
directive. This feature was added in v2.4.0. If you're upgrading from a previous version and you published the Blade views, you should republish them or update them manually.
<x-form wire:submit.prevent="submit">
@wire('debounce.500ms')
<x-form-input name="email" />
@endwire
</form>
Besides the name
attribute, the select
element has a required options
attribute, which should be a simple key-value array.
$countries = [
'be' => 'Belgium',
'nl' => 'The Netherlands',
];
<x-form-select name="country_code" :options="$countries" />
You can provide a slot to the select
element as well:
<x-form-select name="country_code">
<option value="be">Belgium</option>
<option value="nl">The Netherlands</option>
</x-form-select>
If you want a select element where multiple options can be selected, add the multiple
attribute to the element. If you specify a default, make sure it is an array. This applies to bound targets as well.
<x-form-select name="country_code[]" :options="$countries" multiple :default="['be', 'nl']" />
This package has built-in support for BelongsToMany
, MorphMany
, and MorphToMany
relationships. To utilize this feature, you must add both the multiple
and many-relation
attribute to the select element.
In the example below, you can attach one or more tags to the bound video. By using the many-relation
attribute, it will correctly retrieve the selected options (attached tags) from the database.
<x-form>
@bind($video)
<x-form-select name="tags[]" :options="$tags" multiple many-relation />
@endbind
</x-form>
Checkboxes have a default value of 1
, but you can customize it as well.
<x-form-checkbox name="subscribe_to_newsletter" label="Subscribe to newsletter" />
If you have a fieldset of multiple checkboxes, you can group them together with the form-group
component. This component has an optional label
attribute and you can set the name
as well. This is a great way to handle the validation of arrays. If you disable the errors on the individual checkboxes, it will one show the validation errors once. The form-group
component has a show-errors
attribute that defaults to true
.
<x-form-group name="interests" label="Pick one or more interests">
<x-form-checkbox name="interests[]" :show-errors="false" value="laravel" label="Laravel" />
<x-form-checkbox name="interests[]" :show-errors="false" value="tailwindcss" label="Tailwind CSS" />
</x-form-group>
Radio elements behave exactly the same as checkboxes, except the show-errors
attribute defaults to false
as you almost always want to wrap multiple radio elements in a form-group
.
You can group checkbox and radio elements on the same horizontal row by adding an inline
attribute to the form-group
element.
<x-form-group name="notification_channel" label="How do you want to receive your notifications?" inline>
<x-form-radio name="notification_channel" value="mail" label="Mail" />
<x-form-radio name="notification_channel" value="slack" label="Slack" />
</x-form-group>
When you're not using target binding, you can use the default
attribute to mark a radio element as checked:
<x-form-group name="notification_channel" label="How do you want to receive your notifications?">
<x-form-radio name="notification_channel" value="mail" label="Mail" default />
<x-form-radio name="notification_channel" value="slack" label="Slack" />
</x-form-group>
When a validation errors occurs and Laravel redirects you back, the form will be re-populated with the old input data. This old data will override any binding or default value.
This package supports spatie/laravel-translatable
out of the box. You can add a language
attribute to your element.
<x-form-input name="title" language="en" :bind="$book" />
This will result in the following HTML:
<input name="title[en]" value="Laravel: Up & Running" />
To get the validation errors from the session, the name of the input will be mapped to a dot notation like title.en
. This is how old input data is handled as well.
Publish the configuration file and Blade views with the following command:
php artisan vendor:publish --provider="ProtoneMedia\LaravelFormComponents\Support\ServiceProvider"
You can find the Blade views in the resources/views/vendor/form-components
folder. Optionally, in the form-components.php
configuration file, you can change the location of the Blade view per component.
You can bind your own component classes to any of the elements. In the form-components.php
configuration file, you can change the class per component. As the logic for the components is quite complex, it is strongly recommended to duplicate the default component as a starting point and start editing. You'll find the default component classes in the vendor/protonemedia/laravel-form-components/src/Components
folder.
You can define a prefix in the form-components.php
configuration file.
return [
'prefix' => 'tailwind',
];
Now all components can be referenced like so:
<x-tailwind-form>
<x-tailwind-form-input name="company_name" />
</x-tailwind-form>
By the default, the errors messages are positioned under the element. To show these messages, we created a FormErrors
component. You can manually use this component as well. The element takes an optional bag
attribute to specify the ErrorBag
, which defaults to default
.
<x-form>
<x-form-input name="company_name" :show-errors="false" />
<!-- other elements -->
<x-form-errors name="company_name" />
<x-form-errors name="company_name" bag="register" />
</x-form>
The label defaults to Submit, but you can use the slot to provide your own content.
<x-form-submit>
<span class="text-green-500">Send</span>
</x-form-submit>
You can switch to Bootstrap 4 or Bootstrap 5 by updating the framework
setting in the form-components.php
configuration file.
return [
'framework' => 'bootstrap-5',
];
There is a little of styling added to the form.blade.php
view to add support for inline form groups. If you want to change it or remove it, publish the assets and update the view file.
Bootstrap 5 changes a lot regarding forms. If you migrate from 4 to 5, make sure to read the migration logs about forms.
In addition to the Tailwind features, with Bootstrap 4, there is also support for input groups. Use the prepend
and append
slots to provide the contents.
<x-form-input name="username" label="Username">
@slot('prepend')
<span>@</span>
@endslot
</x-form-input>
<x-form-input name="subdomain" label="Subdomain">
@slot('append')
<span>.protone.media</span>
@endslot
</x-form-input>
With Bootstrap 5, the input groups have been simplified. You can add as many items as you would like in any order you would like. Use the form-input-group-text
component to add text or checkboxes.
<x-form-input-group label="Profile" >
<x-form-input name="name" placeholder="Name" id="name" />
<x-form-input-group-text>@</x-form-input-group-text>
<x-form-input name="nickname" placeholder="Nickname" id="nickname" />
<x-form-submit />
</x-form-input-group>
As of Bootstrap 5, you can add floating labels by adding the floating
attribute to inputs, selects (excluding multiple
), and textareas.
<x-form-input label="Floating Label" name="float_me" id="float_me" floating />
You can add block-level help text to any element by using the help
slot.
<x-form-input name="username" label="Username">
@slot('help')
<small class="form-text text-muted">
Your username must be 8-20 characters long.
</small>
@endslot
</x-form-input>
composer test
Please see CHANGELOG for more information about what has changed recently.
Please see CONTRIBUTING for details.
Laravel Analytics Event Tracking
: Laravel package to easily send events to Google Analytics.Laravel Blade On Demand
: Laravel package to compile Blade templates in memory.Laravel Cross Eloquent Search
: Laravel package to search through multiple Eloquent models.Laravel Eloquent Scope as Select
: Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes and constraints by adding them as a subquery.Laravel Eloquent Where Not
: This Laravel package allows you to flip/invert an Eloquent scope, or really any query constraint.Laravel FFMpeg
: This package provides an integration with FFmpeg for Laravel. The storage of the files is handled by Laravel's Filesystem.Laravel Paddle
: Paddle.com API integration for Laravel with support for webhooks/events.Laravel Verify New Email
: This package adds support for verifying new email addresses: when a user updates its email address, it won't replace the old one until the new one is verified.Laravel WebDAV
: WebDAV driver for Laravel's Filesystem.If you discover any security related issues, please email pascal@protone.media instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
参考网址:https://blog.csdn.net/duanshuiliu2017/article/details/81393704 ├── dist │ ├── langs TinyMCE富文本编辑器语言包 │ ├── plugins TinyMCE富文本编辑器组件 │ ├── skins TinyMCE富文本编辑器皮肤 │ └── themes Tin
php artisan key:generate 新的laravle会有密钥不存在的问题,这时候我们执行这句话就可以生成秘钥了 转载于:https://www.cnblogs.com/wzs110/p/11082568.html
modal & component部件 b4-modal-default 注意,form一定要放在table外面,否则submit将没有反应。 <button data-toggle="modal" data-target="#modelId" type="button" class="btn btn-success">添加角色</button> @component('components.mo
如今,开发人员同时处理前端和后端项目是很常见的。根据最近的一项开发人员调查,React Native 是全球最流行的用于构建跨平台移动应用程序的框架之一。Laravel 是一个 PHP Web 框架,具有优雅的代码结构,开发人员可以使用它来创建 REST API。Laravel 目前有71k 的 GitHub 星数,证明了它在开发者社区中的受欢迎程度。 本文将介绍从 React Native 应用
Laravel 5 form builder Form builder for Laravel 5 inspired by Symfony's form builder. With help of Laravels FormBuilder class creates forms that can be easy modified and reused.By default it supports
我面临“类”表单“未找到”的问题,我目前正在使用laravel 5.4。我已经尽了最大的努力来解决这个问题。 谢谢 错误是:哎呀,看起来好像出了什么问题。 1/1 d0b19e04e5a1f8a5507d8ca427362b23807103ca.php行23中的FatalErrorException:在d0b19e04e5a1f8a5507d8ca427362b23807103ca.php行23中
所以我有这个在我的视图中,我想限制文件上载到doc、ppt、pdf、docx、xls、xlsx、txt等。所以我的代码是这样的 但点击后,打开的窗口仍显示视频、音频和其他文件类型。对如何正确地做到这一点有什么想法吗?提前道谢! 如果用户输入的文件类型不被接受,编辑我已经有一个后端检查器。我只是想把用户的选择范围缩小到一个正确的文件类型,当他点击浏览文件。谢谢
1.7.0 新增 从 1.8.0 开始支持blur 时才触发校验以及 debounce,同 Validator 一样也开始支持异步校验。 表单,包含各种输入组件以及对应的校验;我们可以通过数据驱动的方式来生成完成表单。 示例 默认配置使用 一个完整的包含所有的内置表单相关组件。 <cube-form :model="model" :schema="schema" :immediate-
此方法返回'NUMERIC FORM'的当前设置,该设置用于在系统上进行数学计算。 语法 (Syntax) FORM() 参数 (Parameters) 没有 返回值 (Return Value) 此方法返回'NUMERIC FORM'的当前设置,该设置用于在系统上进行数学计算。 例子 (Example) /* Main program */ say FORM() 当我们运行上述程序时,
在大多数Web应用程序中,表单是从用户获取信息的最重要的小部件,例如登录表单/反馈表单,以便可以将值保存在数据库中以供将来参考。 表单窗口小部件用于此目的。 在创建表单之前,我们应该了解xTypes。 xType定义Ext JS UI组件的类型,该组件在呈现组件期间确定。 例如,元素可以是我们将xType作为textField的文本框,或者元素只能具有我们具有Numeric xType的数值。 不