Chapter 5. Using the Form Builder

In this chapter, you will learn how to use Laravel's form builder. The form builder will be demonstrated to facilitate the building of the following elements:

  • Form (open and close)
  • Label
  • Input (text, HTML5 password, HTML5 e-mail, and so on.)
  • Checkbox
  • Submit
  • Anchor tags (href links)

Finally, we'll see an example of how to use the form builder to create the month, date, and year selection elements for the accommodations reservation software form, and how to create a macro to reduce the code duplication.

History

The form builder package in Laravel 4 is called HTML. This was used to help you create HTML, particularly developers who also have to perform web designer duties but prefer to use Laravel facades and helper methods. For example, the following Laravel facade select() method, where the options for the language, British and American English in this example, are passed as an array parameter:

Form::select('language', ['en-us' => 'English (US)','en-gb' => 'English (UK)']);

This can be used as an alternative to the standard HTML, which requires much more repetitious code, as shown in the following code:

<select name="language">
    <option value="en-us">English (US)</option>
    <option value="en-gb">English (UK)</option>
</select>

Since frameworks are constantly evolving, they need to adapt to fulfill the needs of most of their users. Also, whenever possible, they should continue to be more efficient. In some cases, this means rewriting or refactoring pieces of the framework, adding features, or even removing them.

As strange as it may seem, there are several valid reasons for removing the features. The following is a list of reasons for removing packages:

  • To ease the burden and quantity of packages and features that framework core developers need to maintain.
  • To reduce the number of packages that are downloaded and autoloaded.
  • To remove a feature that is not essential.
  • The HTML package was removed from the core of Laravel 5 and is now an external package. In this case, any of the previous reasons could be cited for the reason that this package was removed.
  • HTML, which helps developers build forms, can be used if the frontend developer is also a backend or full-stack developer and prefers Laravel's way of doing things. In other situations, however, the web application HTML interface can be built using a JavaScript framework or a library, such as AngularJS or Backbone.js. In this case, the Laravel form package would not be necessary. Alternatively, as previously stated, Laravel can be used to create an application that is merely a RESTful API. In this case, including the HTML package in the framework core would not be necessary and thus remains auxiliary.

In this particular case, certain Laravel packages were removed to lighten up the overall experience and to move toward a more component-based approach, which is similar to that used in Symfony.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset