Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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: 6 days ago
JSON representation

Form Builder to your Laravel projects

Awesome Lists containing this project

README

        

Laravel Form Builder


Form Builder to your Laravel projects.



Total Downloads
Latest Stable Version
License

# 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'));
}
}
```