2017 © Pedro Peláez
 

library formbuilder

Automatically generate form definitions from SQL Schema

image

pta/formbuilder

Automatically generate form definitions from SQL Schema

  • Wednesday, October 25, 2017
  • by drparham
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2,046 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 36 Versions
  • 25 % Grown

The README.md

FormBuilder

The Form Builder will dynamically generate a 'create' form, and an 'update' form for any given model. The Form Builder will pull down the model's table schema, and map form fields to schema types to determine how to generate a form., (*1)

Installation

To install, run composer require pta/formbuilder on your command line., (*2)

Or you can add the below to your composer.json file manually:, (*3)

"require": {
        "pta/formbuilder": "0.2.*",
    }

Service Provider

You will need to add Pta\Formbuilder\Providers\FormBuilderServiceProvider::class, to your provider array in app.php config., (*4)

Facade

You will need to add 'FormBuilder'=> Pta\FormBuilder\Facades\FormBuilder::class, to your Facades array in app.php config if you want to use the Facade in your views., (*5)

Usage

To use FormBuilder, extend your model with Pta\Formbuilder\Lib\ModelSchemaBuilder., (*6)

In your view where you want to display a Form for your Model, type:, (*7)

{!! FormBuilder::buildForm('Namespace\To\Models\ModelName', 'Method', 'Named Route', 'FormType', ID, 'translation namespace') !!}, (*8)

'ID' and 'translation namespace' can be NULL values., (*9)

Example:, (*10)

This form will create a new User, (*11)

{!! FormBuilder::buildForm('Pta\Formbuilder\Models\User', 'POST', 'User.Create', 'create') !!}, (*12)

This form will update User with an ID of 1, (*13)

{!! FormBuilder::buildForm('Pta\Formbuilder\Models\User', 'POST', 'User.Update', 'update', 1) !!}, (*14)

The forms are built using a series of partial views for each input type, and depending on if it's a 'create' or 'update' form., (*15)

The form's HTML is based off Bootstrap 3 and is completely customizable. Publish the views and customize them however you want., (*16)

Using Translations

FormBuilder allows you to pass a namespace path for using a translation file for form labels. For example, I could have a translation namespace configured for a User Model below:, (*17)

{!! FormBuilder::buildForm('Pta\Formbuilder\Models\User', 'POST', 'User.Update', 'update', 1, 'Pta\Formbuilder\User::') !!}, (*18)

If we wanted to use a Translation file with a 'create' form, we would need to adjust the create example above as follows:, (*19)

{!! FormBuilder::buildForm('Pta\Formbuilder\Models\User', 'POST', 'User.Create', 'create', null, 'Pta\Formbuilder\User::') !!}, (*20)

We will have to pass a NULL value for the ID value., (*21)

Publish Views

php artisan vendor:publish --provider="Pta\Formbuilder\Providers\FormBuilderServiceProvider" --tag="views", (*22)

Overloading Default Behavior

In the config file, we have several areas where we can adjust default behaviors., (*23)

Publish Config

php artisan vendor:publish --provider="Pta\Formbuilder\Providers\FormBuilderServiceProvider" --tag="config", (*24)

Defining NameSpace to your models

The FormBuilder config allows you to define the NameSpace for your models:, (*25)

'entity' => [
        'namespace' => 'App\\',
    ],

After you define your model's namespace in the config you can do the following:, (*26)

{!! FormBuilder::buildForm('User', 'POST', 'User.Create', 'create', null, 'Pta\Formbuilder\User::') !!}, (*27)

If you have your models in multiple places (for instance, multiple packages using FormBuilder), you can still define a fully qualified namespaced class name for the model you want to build a form off of. By default it will check to see if the model exists as passed in. If formbuilder can't find it, it will try using the NameSpace path + model name you passed in to find the model., (*28)

Default Labels

In the config file there is an array of labels. This array is column name => Label. Edit the config file to make adjustments or add more labels., (*29)

You can adjust this list on a per-model basis by declaring a protected $formLabels array in your model., (*30)

    /*
    |--------------------------------------------------------------------------
    | Default Field Labels
    |--------------------------------------------------------------------------
    |
    | This option allows you to define what labels to use as defaults based on
    | standard column name formats. Add or modify to fit your implementation.
    | To override these values on a per model basis, create an array in
    | your Model with the name of $formLabels
    |
    */
    'labels' => [
        'email'             => 'Email Address',
        'email2'            => 'Secondary Email Address',
        'first_name'        => 'First Name',
        'last_name'         => 'Last Name',
        'username'          => 'Username',
        'password'          => 'Password',
        'middle_initial'    => 'Middle Initial',
        'gender'            => 'Gender',
        'address1'          => 'Address',
        'address'           => 'Address',
        'address2'          => 'Address Continued',
        'city'              => 'City',
        'state'             => 'State',
        'zip'               => 'Zip Code',
        'country'           => 'Country',
        'phone'             => 'Phone Number',
        'fax'               => 'Fax Number',
        'dob'               => 'Date of Birth',
        'tos'               => 'Terms of Service',
    ],

Default Skipped form fields

It's likely you don't want every column to show up on your form. The config file has a 'fields' array that consists of column names we don't want to create a form element for., (*31)

    /*
    |--------------------------------------------------------------------------
    | Default Fields to skip populating on form
    |--------------------------------------------------------------------------
    |
    | This option allows you to define what fields/columns to ignore when
    | building out a form. To override these values on a per model basis,
    | create an array in your Model with the name of $skipFields
    |
    */
    'fields' => [
        'created_at',
        'deleted_at',
        'active',
        'updated_at',
        'permissions',
        'last_login',
        'password_date',
        'remember_token',
        'customer_id',
        'all',
    ],

Add or delete items from this list to change form behavior. You can also declare $skipFields in your model, and assign an array of columns to skip on a per model basis., (*32)

Default Form Input Mapping

In the config we also have an array that maps column types, to input types., (*33)

    /*
    |--------------------------------------------------------------------------
    | Default mapping of column types to form types
    |--------------------------------------------------------------------------
    |
    | This option allows you to define the mapping of inputs field types    from
    | the schema to form input types. Example: Varchar is a string, so defaults
    | to Input etc. To override these values on a per model basis, create
    | an array in your Model with the name of $formInputs
    |
    */
    'inputs' => [
        'hidden'            => 'Hidden',
        'varchar'           => 'Input',
        'int'               => 'Input',
        'date'              => 'DateInput',
        'tinyint'           => 'CheckBox',
        'text'              => 'Text',
        'smallint'          => 'CheckBox'
    ],

Adjust this array or add new fields to it to fit your implementation., (*34)

Further Customization

If you have a database field that needs some more customized mapping, you can easily overload the default field type by declaring a a public method with the name of the column with FB_ proceeding it. This will let FormBuilder know it's a FB Method., (*35)

An example of this would be a drop down. If for instance you have a relationship with another model, like a one-to-one, you would want to provide a 'select' field for an 'Int' database field. Typically the 'Int' type will default to a regular Input field., (*36)

public function FB_school_id()
{
    return new SelectField(new School, 'id', 'name');
}

The first parameter is a new instance of the model you want to use to populate the drop down. The second parameter is the column name you want to use as the 'ID' of <option id="$id"> tag. The third parameter is the name you want to use to populate the name of the <option id="$id">{{ $name }}</option> tag., (*37)

Passing a Closure

You can also pass a closure to a SelectField if you want to pass customized data to the select field, and not all of the data in "school" for instance above., (*38)

public function FB_department_id()
    {
        $department = new Department; 
        return new SelectField($department, 'name', function() use ($department) {
            if(is_subclass_of($department, 'Illuminate\Database\Eloquent\Model')){ 
                return $department->where('active',1)->get(array('id','name'));
            }
            return false;
        });
    }

You can also pass a collection to the SelectField if you want to define static options for a select field., (*39)

    public function FB_active()
    {
        $collection = \collect([(object)['name' => 'No', 'id' => "0"],(object)['name'=>'Yes', 'id' => "1"]]);
        return new SelectField(null, 'name', function() use ($collection) {
            return $collection;
        });
    }

Adding form fields

To add form fields that are not part of your table schema, add the following protected array:, (*40)

    protected $addFields = [
        'form_input_id',
    ];

In addition to adding this array, you will need a method on your model of FB_form_input_id() to match the new form fields you're adding. If you add a field and don't provide a method for the field, FormBuilder will throw an error., (*41)

This should allow you to customize the form layout as much as you want, or simply use the default values., (*42)

The Versions

25/10 2017

dev-master

9999999-dev

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

09/06 2017

1.0.4

1.0.4.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

03/06 2017

1.0.3

1.0.3.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

03/06 2017

1.0.2

1.0.2.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

02/06 2017

1.0.1

1.0.1.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

02/06 2017

1.0.0

1.0.0.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

25/10 2016

0.2.10

0.2.10.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

25/10 2016

0.2.9

0.2.9.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

06/10 2016

0.2.8

0.2.8.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

01/10 2016

0.2.7

0.2.7.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

27/09 2016

0.2.5

0.2.5.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

27/09 2016

0.2.4

0.2.4.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

27/09 2016

0.2.3

0.2.3.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

26/09 2016

0.2.2

0.2.2.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

26/09 2016

0.2.1

0.2.1.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

23/09 2016

0.2.0

0.2.0.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

23/09 2016

0.1.21

0.1.21.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

23/09 2016

0.1.20

0.1.20.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

23/09 2016

0.1.18

0.1.18.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

22/09 2016

0.1.17

0.1.17.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

19/09 2016

0.1.16

0.1.16.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

03/11 2015

0.1.15

0.1.15.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

07/10 2015

0.1.14

0.1.14.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

05/09 2015

0.1.13

0.1.13.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

24/07 2015

0.1.12

0.1.12.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

24/07 2015

0.1.11

0.1.11.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

22/07 2015

0.1.9

0.1.9.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

22/07 2015

0.1.10

0.1.10.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

22/07 2015

0.1.8

0.1.8.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

22/07 2015

0.1.7

0.1.7.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

22/07 2015

0.1.6

0.1.6.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

21/07 2015

0.1.4

0.1.4.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

21/07 2015

0.1.3

0.1.3.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

21/07 2015

0.1.2

0.1.2.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

21/07 2015

0.1.1

0.1.1.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham

21/07 2015

0.1.0

0.1.0.0

Automatically generate form definitions from SQL Schema

  Sources   Download

The Requires

 

by Dustin Parham