2017 © Pedro Peláez
 

library html

HTML and Form Builders for the Laravel Framework

image

laravie/html

HTML and Form Builders for the Laravel Framework

  • Saturday, July 28, 2018
  • by crynobone
  • Repository
  • 1 Watchers
  • 16 Stars
  • 47,066 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 29 Versions
  • 9 % Grown

The README.md

Forms & HTML

tests Total Downloads Latest Stable Version Latest Unstable Version License, (*1)

, (*2)

Installation

To install through composer, run the following command from terminal:, (*3)

composer require "laravie/html"

Next, add your new provider to the providers array of config/app.php:, (*4)


'providers' => [ // ... Collective\Html\HtmlServiceProvider::class, ],

Finally, add two class aliases to the aliases array of config/app.php:, (*5)


'aliases' => [ // ... 'Form' => Collective\Html\FormFacade::class, 'Html' => Collective\Html\HtmlFacade::class, ],

Looking to install this package in Lumen? First of all, making this package compatible with Lumen will require some core changes to Lumen, which we believe would dampen the effectiveness of having Lumen in the first place. Secondly, it is our belief that if you need this package in your application, then you should be using Laravel anyway., (*6)

, (*7)

Opening A Form

Opening A Form

{!! Form::open(['url' => 'foo/bar']) !!}
    //
{!! Form::close() !!}

By default, a POST method will be assumed; however, you are free to specify another method:, (*8)

echo Form::open(['url' => 'foo/bar', 'method' => 'put'])

Note: Since HTML forms only support POST and GET, PUT and DELETE methods will be spoofed by automatically adding a _method hidden field to your form., (*9)

You may also open forms that point to named routes or controller actions:, (*10)

echo Form::open(['route' => 'route.name'])

echo Form::open(['action' => 'Controller@method'])

You may pass in route parameters as well:, (*11)

echo Form::open(['route' => ['route.name', $user->id]])

echo Form::open(['action' => ['Controller@method', $user->id]])

If your form is going to accept file uploads, add a files option to your array:, (*12)

echo Form::open(['url' => 'foo/bar', 'files' => true])

, (*13)

CSRF Protection

Adding The CSRF Token To A Form

Laravel provides an easy method of protecting your application from cross-site request forgeries. First, a random token is placed in your user's session. If you use the Form::open method with POST, PUT or DELETE the CSRF token will be added to your forms as a hidden field automatically. Alternatively, if you wish to generate the HTML for the hidden CSRF field, you may use the token method:, (*14)

echo Form::token();

Attaching The CSRF Filter To A Route

Route::post('profile', ['before' => 'csrf', function() {
    //
}]);

, (*15)

Form Model Binding

Opening A Model Form

Often, you will want to populate a form based on the contents of a model. To do so, use the Form::model method:, (*16)

echo Form::model($user, ['route' => ['user.update', $user->id]])

Now, when you generate a form element, like a text input, the model's value matching the field's name will automatically be set as the field value. So, for example, for a text input named email, the user model's email attribute would be set as the value. However, there's more! If there is an item in the Session flash data matching the input name, that will take precedence over the model's value. So, the priority looks like this:, (*17)

  1. Session Flash Data (Old Input)
  2. Explicitly Passed Value
  3. Model Attribute Data

This allows you to quickly build forms that not only bind to model values, but easily re-populate if there is a validation error on the server!, (*18)

Note: When using Form::model, be sure to close your form with Form::close!, (*19)

, (*20)

Form Model Accessors

Laravel's Eloquent Accessor allow you to manipulate a model attribute before returning it. This can be extremely useful for defining global date formats, for example. However, the date format used for display might not match the date format used for form elements. You can solve this by creating two separate accessors: a standard accessor, and/or a form accessor., (*21)

To define a form accessor, create a formFooAttribute method on your model where Foo is the "camel" cased name of the column you wish to access. In this example, we'll define an accessor for the date_of_birth attribute. The accessor will automatically be called by the HTML Form Builder when attempting to pre-fill a form field when Form::model() is used., (*22)

, (*23)

Labels

Generating A Label Element

echo Form::label('email', 'E-Mail Address');

Specifying Extra HTML Attributes

echo Form::label('email', 'E-Mail Address', ['class' => 'awesome']);

Note: After creating a label, any form element you create with a name matching the label name will automatically receive an ID matching the label name as well., (*24)

, (*25)

Text, Text Area, Password & Hidden Fields

Generating A Text Input

echo Form::text('username');

Specifying A Default Value

echo Form::text('email', 'example@gmail.com');

Note: The hidden and textarea methods have the same signature as the text method., (*26)

Generating A Password Input

echo Form::password('password', ['class' => 'awesome']);

Generating Other Inputs

echo Form::email($name, $value = null, $attributes = []);
echo Form::file($name, $attributes = []);

, (*27)

Checkboxes and Radio Buttons

Generating A Checkbox Or Radio Input

echo Form::checkbox('name', 'value');

echo Form::radio('name', 'value');

Generating A Checkbox Or Radio Input That Is Checked

echo Form::checkbox('name', 'value', true);

echo Form::radio('name', 'value', true);

, (*28)

Number

Generating A Number Input

echo Form::number('name', 'value');

, (*29)

Date

Generating A Date Input

echo Form::date('name', \Carbon\Carbon::now());

, (*30)

File Input

Generating A File Input

echo Form::file('image');

Note: The form must have been opened with the files option set to true., (*31)

, (*32)

Generating A Drop-Down List

echo Form::select('size', ['L' => 'Large', 'S' => 'Small']);

Generating A Drop-Down List With Selected Default

echo Form::select('size', ['L' => 'Large', 'S' => 'Small'], 'S');

Generating a Drop-Down List With an Empty Placeholder

This will create an <option> element with no value as the very first option of your drop-down., (*33)

echo Form::select('size', ['L' => 'Large', 'S' => 'Small'], null, ['placeholder' => 'Pick a size...']);

Generating A Grouped List

echo Form::select('animal', [
    'Cats' => ['leopard' => 'Leopard'],
    'Dogs' => ['spaniel' => 'Spaniel'],
]);

Generating A Drop-Down List With A Range

echo Form::selectRange('number', 10, 20);

Generating A List With Month Names

echo Form::selectMonth('month');

, (*34)

Buttons

Generating A Submit Button

echo Form::submit('Click Me!');

Generating A Button

echo Form::button('Click Me!');

Note: Default value for third parameter is true. As default, any value that you pass will be escaped., (*35)

echo Form::button('<i class="fa fa-trash><i> Destroy"', ['class' => 'btn btn-danger'], false);

, (*36)

Custom Macros

Registering A Form Macro

It's easy to define your own custom Form class helpers called "macros". Here's how it works. First, simply register the macro with a given name and a Closure:, (*37)

Form::macro('myField', function() {
    return '<input type="awesome">';
});

Now you can call your macro using its name:, (*38)

Calling A Custom Form Macro

echo Form::myField();

, (*39)

Custom Components

Registering A Custom Component

Custom Components are similar to Custom Macros, however instead of using a closure to generate the resulting HTML, Components utilize Laravel Blade Templates. Components can be incredibly useful for developers who use Twitter Bootstrap, or any other front-end framework, which requires additional markup to properly render forms., (*40)

Let's build a Form Component for a simple Bootstrap text input. You might consider registering your Components inside a Service Provider's boot method., (*41)

Form::component('bsText', 'components.form.text', ['name', 'value', 'attributes']);

Notice how we reference a view path of components.form.text. Also, the array we provided is a sort of method signature for your Component. This defines the names of the variables that will be passed to your view. Your view might look something like this:, (*42)

// resources/views/components/form/text.blade.php
<div class="form-group">
    {{ Form::label($name, null, ['class' => 'control-label']) }}
    {{ Form::text($name, $value, array_merge(['class' => 'form-control'], $attributes)) }}
</div>

Custom Components can also be created on the Html facade in the same fashion as on the Form facade., (*43)

Providing Default Values

When defining your Custom Component's method signature, you can provide default values simply by giving your array items values, like so:, (*44)

Form::component('bsText', 'components.form.text', ['name', 'value' => null, 'attributes' => []]);

Calling A Custom Form Component

Using our example from above (specifically, the one with default values provided), you can call your Custom Component like so:, (*45)

{{ Form::bsText('first_name') }}

This would result in something like the following HTML output:, (*46)

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

, (*47)

Generating URLs

Generate a HTML link to the given URL., (*48)

echo link_to('foo/bar', $title = null, $attributes = [], $secure = null);

Generate a HTML link to the given asset., (*49)

echo link_to_asset('foo/bar.zip', $title = null, $attributes = [], $secure = null);

Generate a HTML link to the given named route., (*50)

echo link_to_route('route.name', $title = null, $parameters = [], $attributes = []);

Generate a HTML link to the given controller action., (*51)

echo link_to_action('HomeController@getIndex', $title = null, $parameters = [], $attributes = []);

The Versions

28/07 2018

5.6.x-dev

5.6.9999999.9999999-dev https://github.com/laravie/html

HTML and Form Builders for the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adam Engebretson

laravel form html

20/06 2018
24/05 2018
08/02 2018

5.3.x-dev

5.3.9999999.9999999-dev https://github.com/laravie/html

HTML and Form Builders for the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adam Engebretson

laravel form html

27/05 2017

5.2.x-dev

5.2.9999999.9999999-dev https://github.com/laravie/html

HTML and Form Builders for the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adam Engebretson

laravel form html

27/05 2017

5.1.x-dev

5.1.9999999.9999999-dev

HTML and Form Builders for the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adam Engebretson

26/01 2017
27/11 2016
26/08 2016
21/06 2016
16/02 2016
26/12 2015

v5.1.5

5.1.5.0

HTML and Form Builders for the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adam Engebretson

21/12 2015
21/12 2015

v5.1.4

5.1.4.0

HTML and Form Builders for the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adam Engebretson

25/11 2015

v5.1.3

5.1.3.0

HTML and Form Builders for the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adam Engebretson

19/11 2015

v5.1.2

5.1.2.0

HTML and Form Builders for the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adam Engebretson

06/11 2015

v5.1.1

5.1.1.0

HTML and Form Builders for the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adam Engebretson

02/11 2015

v5.1.0

5.1.0.0

HTML and Form Builders for the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adam Engebretson