12

I am currently learning Laravel and finding it really useful and interesting.

At the moment I am making a simple online application form.

What are the biggest advantages to doing things using the Laravel syntax like:

{{ Form::open(array('url' => 'foo/bar')) }}

As opposed to simply:

<form action="foo/bar">

Or:

echo Form::text('username');

Instead of:

<input type="text" name="username" />

The Laravel way must be better, I just wish to know why exactly?

imperium2335
  • 23,402
  • 38
  • 111
  • 190
  • 1
    One excellent advantage I find is that if you do something like `Redirect::back()->withInput();`, it will redirect back to your form and automatically fill it with the previously entered values for you – Jonathon Sep 22 '14 at 08:39
  • Additionally, you can fill those forms with model data automatically, by using `{{ Form::model('foo') }}` [link](http://laravel.com/docs/html#form-model-binding) – Allmighty Sep 22 '14 at 08:55

2 Answers2

11

Using built-in HTML helpers have many benefits:

  1. Using Form::open you add CSRF protection input hidden (by default)

  2. Using form elements (inputs/textarea etc.) and withInput method for Redirection allows you to easily fill in the form with the same data with almost no coding

    If you use Redirect::route('form'->withInput(); and have input text {{Form::text('username')}} it will automatically set input's value the old data - you don't need to code it yourself checking it

  3. Also if you want to match fields with labels its much easier:

    {{ Form::label('username', 'Enter username') }}
    {{ Form::text('username') }}
    

    it will generate the following code:

    <label for="username">Enter username</label>
    <input name="username" type="text" id="username">
    

    so as you see id will be created automatically

Probably there are some more. However the main disadvantage is that you need to learn and it's not portable in case you want to move your site to other Framework but each solution has pros and cons.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • So the ->withInput is useful for when the user has forgotten a required field and is sent back to the form, with all other data they entered filled out? – imperium2335 Sep 22 '14 at 09:05
  • @imperium2335 Yes, if you validate user input (and you usually do) and some field are missing or in wrong format you redirect using withInput and all fields will be automatically filled with previous values without any extra coding – Marcin Nabiałek Sep 22 '14 at 09:08
  • Is there a way to get Laravel to remember the options chosen for selects? I have it remembering for every other kind of field but it seems to not work with selects. – imperium2335 Sep 22 '14 at 18:59
  • @imperium2335 Open another question with your code and detailed information what's not working because it should also work for selects – Marcin Nabiałek Sep 22 '14 at 19:05
  • I will have another go tonight and if it still isn't working I'll do as you suggest, thanks. – imperium2335 Sep 23 '14 at 06:05
  • For 1. you can just add `{{ Form::token() }}` into your code for the same effect. For 3. I find them much of a muchness. 2. Is very handy, though. – Chuck Le Butt May 25 '17 at 15:40
5

There are so many advantages of using Laravel's Form component but one useful advantage is that, when you just use this:

{{ Form::open(array('url' => 'foo/bar')) }}

{{ Form::close() }}

It automatically appends a hidden _token field which is useful for CSRF protection. otherwise you have to manually create the _token field using echo Form::token() or other way maybe. Also, when you use RESTful routes then Laravel's Form component appends the corresponding hidden _method field as well. Following note is taken from Laravel website:

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.

There are also other advantages like Form Model Binding, generating form elements (specially select) easily and many more. Read more about Form on documentation.

BTW, the Redirect::back()->withInput() doesn't deppend only on use of Form component, if you use something like this, for example:

<input type='text' name='username' value='<?php echo Input::old('username') ?>' />

This will still work, the field will be repopulated on redirect back with inputs.

The Alpha
  • 143,660
  • 29
  • 287
  • 307