2017 © Pedro Peláez
 

library blade-forms

A set of Laravel Blade files which generate HTML forms

image

gridprinciples/blade-forms

A set of Laravel Blade files which generate HTML forms

  • Sunday, September 13, 2015
  • by gbrock
  • Repository
  • 1 Watchers
  • 0 Stars
  • 76 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

CSS-agnostic form rendering via Laravel Blade

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads, (*1)

A set of Laravel Blade components useful for rendering basic, fully-accessible HTML forms in a Laravel app. The approach is barebones: CSS and JS is not included, but Tailwind (and possibly other framework) presets are planned., (*2)

Installation

You can install the package via composer:, (*3)

composer require gridprinciples/blade-forms

You can publish the config file with:, (*4)

php artisan vendor:publish --tag="blade-forms-config"

Optionally, you can publish the views using, (*5)

php artisan vendor:publish --tag="blade-forms"

Usage

<x-form :post="route('form-submission-route')">

    <x-form::input 
        name="name" 
        label="Your name" 
        help="What should we call you?"
        required
        />

    <x-form::radio-buttons 
        name="weekday"
        label="Preferred weekday" 
        :options="[
            'mo' => 'Monday',
            'tu' => 'Tuesday',
            'we' => 'Wednesday',
            'th' => 'Thursday',
            'fr' => 'Friday',
        ]"
        required
        />

    <x-form::checkbox
        name="agree"
        label="I agree with the terms"
        />

</x-form>

Result:, (*6)

Example of Blade forms rendered output, (*7)

Text Input

Text inputs are, perhaps, the simplest form element. As with all elements, we can call it using the x-form Blade prefix:, (*8)

<x-form::input name="username" />

We can continue to decorate this input (and apply some readable indentation):, (*9)

<x-form::input
    name="username"
    label="Please provide your username"
    help="If you don't have a username yet, use your email."
    required
    />

This gives us an input that's ready to use:, (*10)

Example input, (*11)

We can further customize the attributes of the wrapping <div>s and the <label>:, (*12)

<x-form::input
    name="username"
    label="Please provide your username"
    help="If you don't have a username yet, use your email."
    required
    :wrapperAttributes="[
        'class' => 'form-group-wrapper', 
        'id' => 'username-field',
    ]"
    :labelAttributes="['class' => 'form-label']"
    :inputGroupAttributes="['class' => 'form-input-wrapper']"
    />

Results in:, (*13)

<div class="form-group-wrapper" id="username-field">
    <label for="username_01" class="form-label">
        Please provide your username*
        <span class="sr-only">(required)</span>
    </label>
    <div class="form-input-wrapper">
        <input id="username_01" name="username" type="text" aria-describedby="username_01_feedback" required="">
    </div>
    <div id="username_01_feedback">
        <div>If you don't have a username yet, use your email.</div>
    </div>
</div>

Textarea

The textarea is used nearly identically to the Input component above:, (*14)

<x-form::textarea
    name="message"
    label="What would you like to say?"
    rows="2"
    />

Example textarea, (*15)

Radio buttons

Radio controls should be rendered as a set, with options:, (*16)

<x-form::radio-buttons
    name="choice"
    label="Do you think so?"
    :options="[
        'yes' => 'Yes',
        'no' => 'No',
        'maybe' => 'Maybe',
    ]"
    value="maybe" {{-- pre-selected option --}}
    />

Example radio buttons, (*17)

You can customize the option attributes by passing an array instead of a string as the label. Here's how to disable the "Maybe" option:, (*18)

<x-form::radio-buttons
    name="choice"
    label="Do you think so?"
    :options="[
        'yes' => 'Yes',
        'no' => 'No',
        'maybe' => [
            'label' => 'Maybe',
            'disabled' => true,
        ],
    ]"
    />

Checkbox

A single checkbox can stand alone:, (*19)

<x-form::checkbox
    name="remember"
    label="Remember me"
    id="remember_checkbox"
    />

Example checkbox, (*20)

Or, you can render a list of options:, (*21)

<x-form::checkbox-list
    name="fields"
    label="Which fields can be exported?"
    :options="[
        'name' => 'Full name',
        'email' => 'Email address',
        'phone' => 'Phone number',
        'zip' => 'ZIP code',
    ]"
    />

Example checkbox, (*22)

Select

Select menu inputs must have options, and can be written in a very similar way to radio and checkbox lists:, (*23)

<x-form::select
    name="frequency"
    label="How frequently would you like to receive updates?"
    :options="[
        'sync' => 'As soon as anything happens',
        'daily' => 'Daily updates',
        'weekly' => 'Weekly updates',
        'monthly' => 'Monthly updates',
    ]"
    />

Example select input, (*24)

Form

You can optionally create <form> elements using this component, which includes some shortcuts to make things cleaner. Laravel apps typically have something like this:, (*25)

<form method="POST" action="{{ route('submission-route') }}">
    @csrf
    @method('PUT')
    {{-- form elements --}}
</form>

Using the <x-form> component, we can simplify things:, (*26)

<x-form :put="route('submission-route')">
    {{-- form elements --}}
</x-form>

The component will automatically: - add the CSRF field - add the method based on which variable you fill with your submission URL. You can pass your URL using get, post, put, patch, or delete, and the method will be set for you., (*27)

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently., (*28)

Contributing

Please see CONTRIBUTING for details., (*29)

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities., (*30)

Credits

License

The MIT License (MIT). Please see License File for more information., (*31)

The Versions

13/09 2015

dev-master

9999999-dev

A set of Laravel Blade files which generate HTML forms

  Sources   Download

MIT

The Requires

 

23/07 2015

0.1

0.1.0.0

A set of Laravel Blade files which generate HTML forms

  Sources   Download

MIT

The Requires