Fastforms
Quick and easy form and view creation for laravel models, with validations.
Setup
Install from composer.
composer require lintaba/laravel-fastforms
Add its trait to your controller:
<?php
use Illuminate\Http\Request;
use Illuminate\View\View;
class ItemController
{
# 1. add fastform's trait
use \lintaba\fastforms\HasFormTrait;
# 2. set up your show, edit, update, create, store methods as needed.
public function show(Item $item) : View{
$form = $this->getForm();
return view('item.show', compact('item', 'form'));
}
public function edit(Item $item) : View{
$form = $this->getForm();
return view('item.edit', compact('item', 'form'));
}
public function create() : View{
$form = $this->getForm();
return view('item.create', compact('form'));
}
public function update(Request $request, Item $item)
{
$requestData = $this->getValidInput($request, $extraValidationRules = [], $item);
$item->fill($requestData);
$item->save();
return redirect()->back();
}
public function store(Request $request)
{
$requestData = $this->getValidInput($request, $extraValidationRules = []);
$item= new Item;
$item->fill($requestData);
$item->save();
return redirect()->back();
}
# 3.
public function getForm($id = null){
return [
['field'=>'name', 'type'=>'text'],
//...
];
}
}
For your view, you should add the following:
Create:
@include('Fastforms::form', compact('form'))
View:
@include('Fastforms::view', compact('form', 'item'))
Edit:
@include('Fastforms::form', compact('form', 'item'))
Template options
Name | Type | Description |
---|---|---|
form |
components[] |
required, recieved from getForm() |
item |
Entity / ArrayAccess |
either the edited/showed entity, or a similar array. |
tag |
string |
Defines localization group. If not provided, it defaults to the item's singular name, eg the name of \App\Users will be translated by using user.name . Can be overriden on component level. Useful if you are using it without models. |
Components
Each form entry can be:
- A string, as a category label.
- An array, that sets up a component.
#####Each component have the following properties:
Name | Type | Description |
---|---|---|
field |
string |
required the related property path of the model. ie. name , but it supports, dot notation too, like extra_attributes.looks_like_a_potato , if the attribute is casted to object or array. |
type |
string |
required anything from available controller types . |
virtual |
boolean |
if defined as true , it will not be processed. Usefull for virtual fields, that you controll separetly in the controller. |
disabled |
boolean |
true means it will not be writable, will not be stored. |
nullable |
boolean |
true means its value can become null . Usefull ie. for the reference type, and anywhere else where you'd rather store null on the field instead of its empty value. |
uncheckedWrite |
boolean |
by default, we check if a model property is defined as fillable . This behavior is turned off, if it is defined as true . Otherwise an LogicException will be thrown |
required |
boolean |
denotes that the field is required, and should have a non-empty value. |
label |
string |
sets the label value of the field. If not defined, it will autogenerated, like trans("user.name") . |
tag |
string |
overwrites how field labels are rendered for localization, if no label is explicitly defined. |
default |
string |
default value. |
validation |
string / array | laravel validation rules for the given dataset. |
#####Most component have the following properties:
Name | Type | Description |
---|---|---|
if |
string |
if defined, hides the field, shows only if the other defined field is not null. Needs js. #TODO |
class |
string |
extra css classes for the field (or all of its subfields). |
attributes |
string |
extra Form::text attributeses for the field (or all of its subfields). |
description |
string |
htmlable extra textual description, usually below the field. |
#####Available controller types:
- address
- boolean
- button
- date
- datetime
- json
- (label)
- listarray
- matrix
- multiselect
- number
- password
- rangeRepeater
- reference
- repeater
- select
- text
- textarea
- textWithCheckbox
###Options
#####address
Based on Form::select
and Form::text
.
Consist of multiple subfields:
- country
- zip
- city
- street
- house
- extra
for any field
its stored and retrieverd as field_country
, field_zip
, etc.
Name | Type | Description |
---|---|---|
countries |
array |
list of available countries. If not specified, it will use the default. See below. |
class_* |
string |
extra css classes for * subfield. |
attributes_* |
string |
extra Form::text attributeses for * subfield. |
default[*] |
array |
default value of the given subfield |
#####boolean
Based on Form::checkbox
.
Name | Type | Description |
---|---|---|
truthValue |
string |
value of checked item. Defaults to 1 . |
#####button Simple html button
Name | Type | Description |
---|---|---|
role |
string of bootstrap level | defaults to default . can be warning ,success ,danger , etc. |
button |
string |
required the id of the button. |
Extra translations:
$tag . $button . '.label'
$tag . $button . '.title'
#####date
Built uppon Form::text
of type date
. Needs js #TODO
Data should implement ?DateTimeInterface
Name | Type | Description |
---|---|---|
prepend |
string |
If defined, it will be added as a prepended text on the input, as it works with bootstrap. |
today |
boolean |
If defined, a today button will be added to the end fo the input. |
format |
string |
php formatstring. Defaults to Y-m-d . |
min |
string |
begining date |
#####datetime
Built uppon Form::text
of type datetime
. Needs js #TODO
Data should implement ?DateTimeInterface
Name | Type | Description |
---|---|---|
prepend |
string |
If defined, it will be added as a prepended text on the input, as it works with bootstrap. |
today |
boolean |
If defined, a today button will be added to the end fo the input. |
format |
string |
php formatstring. Defaults to Y-m-d H:i:s . |
min |
string |
begining date |
#####json
Built uppon Form::textarea
. Validates against regex rules.
#####listarray Multiple radio based selects. Usefull for score scales, rights, multiple yes/no's.
Note: If for some reason the current value is not exists (eg. definiton has been changed, but data hasnt been migrated), an extra option will be visible with the current value.
Name | Type | Description |
---|---|---|
options |
string[] |
required List of possible values. (column definition) |
source |
string[] |
required List of data values. (row definition) |
labels |
string[] |
translated label for each row. |
#####matrix
Matrix of checkboxes. Stored like field.1_2 = 1
.
Name | Type | Description |
---|---|---|
source |
string[] |
required List of data values. (row and column definition) |
labels |
string[] |
translated label for each row. |
key |
string |
format of key. defaults to %s_%s (vertical_horizontal) |
#####multiselect
Based on Form::select
. Multiple options select.
Name | Type | Description |
---|---|---|
options |
array |
Possibly selectable options. |
trans |
string |
If set, it will translate all options, with the provided prefix. |
#####number
Based on Form::text
. Shows a steppable numeric field. Used for incremental-styled data.
Name | Type | Description |
---|---|---|
prepend |
string |
prepends something before the number (eg. $) |
append |
string |
appends something after the number (eg. pcs) |
min |
int |
minimal value. defaults to 0 . |
max |
int |
maximal value. Defaults to 1 000 000 . |
step |
float | step value. Defaults to 1 . |
#####password
Based on Form::password
.
#####rangeRepeater List to define values for given ranges. Eg. price should be 0 for distances of 1-10 km, and 100 for further. Note: TODO, fancy format. Note: Needs js.
#####reference
References to an external entry, basicly a belongsTo
.
Name | Type | Description |
---|---|---|
builder |
QueryBuilder | Laravel QueryBuilder to make the query. |
id |
string |
id field. Defaults to id . |
attr |
string |
shown attr of the models. Can use virtual fields only if an Eloquent Query Builder has been passed. |
#####repeater
An array of values. Extendable list. Format: item[i][subfield]
.
Name | Type | Description |
---|---|---|
subfield |
string |
property where the field belongs inside parent collection. |
max |
int |
maxlength attribute of the field |
#####select
Based on Form::select
.
Name | Type | Description |
---|---|---|
options |
array |
Possibly selectable options. |
trans |
string |
If set, it will translate all options, with the provided prefix. |
#####text
Based on Form::text
.
Note: Only one of
prepend
,prepend_btn
is available at the same time.
Note: Only one of
append
,append_btn
,fillfrom
is available at the same time.
Name | Type | Description |
---|---|---|
prepend |
?string | prepends something before the input |
prepend_btn |
?string | prepends a button before the input, with a class of btn-$prepend_btn , that you may add custom code to handle. |
append |
?string | appends something afterthe input |
append_btn |
?string | appends a button after the input, with a class of btn-$append_btn , that you may add custom code to handle. |
max |
int |
maxlength attribute of the field |
fillfrom |
?string | Adds a button to copy the content of another field to the current field. |
Translations:
$tag.$prepend_btn.'.title'
$tag.$prepend_btn.'.text'
$tag.$append_btn.'.title'
$tag.$append_btn.'.text'
#####textarea
Based on Form::textarea
Name | Type | Description |
---|---|---|
rows |
int |
row count. Defaults to 2 . |
#####textWithCheckbox
Same as text
, but there is an exta checkbox
at the end of it, that is visible only when the text
is filled with some data.
Name | Type | Description |
---|---|---|
sub |
component | checkbox component definition fo a checkbox. |
##Validation
Inputted data gets automatically validated, based on what is being provided in its validation
property.