https://github.com/balajidharma/laravel-form-builder
Form Builder to your Laravel projects
https://github.com/balajidharma/laravel-form-builder
laravel laravel-form laravel-form-builder laravel-framework laravel-package
Last synced: 3 months ago
JSON representation
Form Builder to your Laravel projects
- Host: GitHub
- URL: https://github.com/balajidharma/laravel-form-builder
- Owner: balajidharma
- License: other
- Created: 2024-07-25T02:09:30.000Z (10 months ago)
- Default Branch: 1.x
- Last Pushed: 2024-11-07T23:07:03.000Z (7 months ago)
- Last Synced: 2025-02-01T04:41:20.578Z (4 months ago)
- Topics: laravel, laravel-form, laravel-form-builder, laravel-framework, laravel-package
- Language: PHP
- Homepage:
- Size: 1.11 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Laravel Form Builder
Form Builder to your Laravel projects.
# Laravel Form Builder
Laravel Form builder is forked from [kristijanhusak/laravel-form-builder](https://github.com/kristijanhusak/laravel-form-builder).
## Table of Contents
- [Installation](#installation)
- [Demo](#demo)
- [Quick start](#quick-start)## Installation
### Using Composer
```sh
composer require balajidharma/laravel-form-builder
```Or manually by modifying `composer.json` file:
``` json
{
"require": {
"balajidharma/laravel-form-builder": "1.*"
}
}
```And run `composer install`
## Demo
The "[Basic Laravel Admin Penel](https://github.com/balajidharma/basic-laravel-admin-panel)" starter kit come with Laravel Form Builder## Quick start
Creating form classes is easy. With a simple artisan command:
```sh
php artisan make:form Forms/SongForm --fields="name:text, lyrics:textarea, publish:checkbox"
```Form is created in path `app/Forms/SongForm.php` with content:
```php
add('name', Field::TEXT, [
'rules' => 'required|min:5'
])
->add('lyrics', Field::TEXTAREA, [
'rules' => 'max:5000'
])
->add('publish', Field::CHECKBOX);
}
}
```If you want to instantiate empty form without any fields, just skip passing `--fields` parameter:
```sh
php artisan make:form Forms/PostForm
```Gives:
```php
create(\App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);return view('song.create', compact('form'));
}public function store(FormBuilder $formBuilder)
{
$form = $formBuilder->create(\App\Forms\SongForm::class);if (!$form->isValid()) {
return redirect()->back()->withErrors($form->getErrors())->withInput();
}// Do saving and other things...
}
}
```Alternative example:
```php
create(SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);return view('song.create', compact('form'));
}public function store(FormBuilder $formBuilder)
{
$form = $formBuilder->create(SongForm::class);if (!$form->isValid()) {
return redirect()->back()->withErrors($form->getErrors())->withInput();
}// Do saving and other things...
}
}
```If you want to store a model after a form submit considerating all fields are model properties:
```php
create(\App\Forms\SongForm::class);
$form->redirectIfNotValid();
SongForm::create($form->getFieldValues());// Do redirecting...
}
```You can only save properties you need:
```php
create(\App\Forms\SongForm::class);
$form->redirectIfNotValid();
$songForm = new SongForm();
$songForm->fill($request->only(['name', 'artist'])->save();// Do redirecting...
}
```Or you can update any model after form submit:
```php
getForm($songForm);
$form->redirectIfNotValid();$songForm->update($form->getFieldValues());
// Do redirecting...
}
```Create the routes
```php
// app/Http/routes.php
Route::get('songs/create', [
'uses' => 'SongsController@create',
'as' => 'song.create'
]);Route::post('songs', [
'uses' => 'SongsController@store',
'as' => 'song.store'
]);
```Print the form in view with `form()` helper function:
```html
@extends('app')
@section('content')
{!! form($form) !!}
@endsection
```Go to `/songs/create`; above code will generate this html:
```html
Name
Lyrics
Publish
```
Or you can generate forms easier by using simple array
```php
createByArray([
[
'name' => 'name',
'type' => Field::TEXT,
],
[
'name' => 'lyrics',
'type' => Field::TEXTAREA,
],
[
'name' => 'publish',
'type' => Field::CHECKBOX
],
]
,[
'method' => 'POST',
'url' => route('song.store')
]);return view('song.create', compact('form'));
}
}
```