lintaba/laravel-fastforms

Fast form definition for laravel models with validation and views

dev-master 2020-06-01 22:18 UTC

This package is auto-updated.

Last update: 2024-10-29 06:05:48 UTC


README

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

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:

#####Most component have the following properties:

#####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.

#####boolean Based on Form::checkbox.

#####button Simple html button

Extra translations:

  • $tag . $button . '.label'
  • $tag . $button . '.title'

#####date Built uppon Form::text of type date. Needs js #TODO Data should implement ?DateTimeInterface

#####datetime Built uppon Form::text of type datetime. Needs js #TODO Data should implement ?DateTimeInterface

#####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.

#####matrix Matrix of checkboxes. Stored like field.1_2 = 1.

#####multiselect Based on Form::select. Multiple options select.

#####number

Based on Form::text. Shows a steppable numeric field. Used for incremental-styled data.

#####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.

#####repeater An array of values. Extendable list. Format: item[i][subfield].

#####select

Based on Form::select.

#####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.

Translations:

  • $tag.$prepend_btn.'.title'
  • $tag.$prepend_btn.'.text'
  • $tag.$append_btn.'.title'
  • $tag.$append_btn.'.text' #####textarea

Based on Form::textarea

#####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.

##Validation Inputted data gets automatically validated, based on what is being provided in its validation property.