Code Monkey home page Code Monkey logo

formfields's Introduction

FormFields

FormFields is a collection of editable Laravel Blade views to render bootstrap responsive HTML content for individual form fields. The form configuration is fetched from ORM model.

It also contains a class to queue linked and inline supporting javascripts.

Main features

  • read form fields configurtion directly from ORM models
  • render individual HTML content for form fields
  • support for required javascripts included in form fields template
  • support for Laravel Blade template engine

Setup

Add to your config/app.php

'providers' => [
	...
    MarkHilton\FormFields\FormFieldsServiceProvider::class,
],

'aliases' => [
	...
    'jsQueue'     => MarkHilton\FormFields\jsQueue::class,
    'FormBuilder' => MarkHilton\FormFields\FormBuilder::class,
],

inside app root folder copy config file:

cp vendors/markhilton/formfields/config/formfields.php configs/formfields.php

Model

class Your_Model_Name extends Model
{
    public static $errors = [];

    // ... //

    // HTML form builder definitions
    public static $form = [
        'name' => [ 
            'type'         => 'input',
            'label'        => 'Label name',
            'position'     => 'top',
        ],

        'status'           => [ 
            'type'         => 'select',
            'label'        => 'Status',
            'position'     => 'side',
            'choice'       => [
              'active'   => 'Active',
              'pending'  => 'Pending',
              'suspended'=> 'Suspended',
            ],
            'default'      => 'active',
        ],

    // ... //

Controller

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Your_Model_Name;
use Illuminate\Http\Request;

class SiteController extends Controller {

    public function create(Request $request)
    {
        $form = \FormBuilder::build(Your_Model_Name::$form, $request->old() ? $request->old() : []),

        return view('layout', $form);

    // ... //

View

<div class="row">
    {{ $status }}

    {{ $name }}
</div>

HTML output

<div class="row">
    <div>
        <select id="field-status" class="width100p" name="status" data-placeholder="">
            <option selected="selected" value="active">Active</option>
            <option value="pending">Pending</option>
            <option value="suspended">Suspended</option>
        </select>
    </div>

    <div class="form-group">
        <label class=" control-label">Name</label>
        <div>
            <input type="text" name="name" value="" class="form-control" />
        </div>
    </div>
</div>

Usage

Queue for linked and inline javascripts inside view template

Controller

  • jsQueue::push('[javascript code]'); to push javascripts code to the queue
  • jsQueue::link('[javascript file]'); to add linked javascripts files

View

add {{ jsQueue::render() }} call in template header or footer in order to render queued javascripts

How to steps

  1. Define $form property in ORM model
  2. Assign HTML form fields output from \FormBuilder::build(ORM_MODEL::$form);
  3. Render form field in the template with {{ $field_name }}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.