php

Creating a Form Input Field

Problem

You want to create a form input field.

Instead of writing HTML directory you want to use Laravel’s Form facade.

Solution

Use the Form::input() method.

The method takes 4 arguments.

$type – (required) The first argument specifies the type of input. Values such as “text”, “password”, “file”, etc. are accepted.
$name – (required) The second argument is name.
$value – (optional) The third argument is the value for the input field.
$options – (optional) The fourth argument is an array of additional field attributes. The array can be populated with items having keys such as “id”, “size”, or “class”.
Usually, this is used in a Blade template.

{{ Form::input('text', 'name') }}
{{ Form::input('email', 'email_address', null, ['class' => 'emailfld']) }}

Discussion

Use the specific method for the type of field you want.

Instead of calling Form::input() directly, use one of the following:

Form::password() – Creating a Password Input Field.
Form::text() – Creating a Text Input Field.
Form::hidden() – Creating a Hidden Input Field.
Form::email() – Creating an Email Input Field.
Form::url() – Creating a URL Input Field.
Form::file() – Creating a File Input Field.

Model binding

See the Creating a New Model Based Form recipe for details on how the input value is overridden if you bind a model to the form.

About the author

laravelrecipies