Elementary Laravel:
Forms

Forms are an important part of any web application. I'll show you a quick and easy way to create a form with helpers.



Introduction

As mentioned in the previous lesson, we will now create a contact form. We will use normal HTML with some Laravel helpers in order to provide a better user experience.

Improve your skills! To improve upon the form that we will create in this lesson I advise you to take a look at Forms & HTML package. I have an in-depth lesson about it on this website called Laravel Forms & HTML so be sure to check it out.

Create the form

We will use basic Bootstrap styling to design the form. We will require from the user to enter his:

  • name
  • email
  • message (comment - We will cover at a later point)

Improve your skills! It is very important to know how to build and design forms with Boostrap, so I recommend reading the documentation about it.

In our contact.blade.php you will see a Blade comment {{-- Contact form goes here --}}. Replace that line with the following:

<form method="POST" action="{{ url('/contact') }}">
    {{ csrf_field() }}
    <div class="form-group">
        <label for="name">Name</label>
        <input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" placeholder="Your name">
    </div>
    <div class="form-group">
        <label for="email">E-mail</label>
        <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" placeholder="Your E-mail">
    </div>
    <div class="form-group">
        <label for="comment">Message</label>
        <textarea rows="10" id="comment" class="form-control" name="comment" placeholder="Your message">{{ old('comment') }}</textarea>
    </div>
    <button type="submit" class="btn btn-primary btn-lg">Send</button>
</form>

I will explain the helpers used here in the following chapter, but for now save the changes and open your browser to http://localhost:8000/contact. You will see that the page looks like this now:

Contact with form

We have specified that we want to POST the data from the form to /contact URL. If you remember our routes file from a few lessons ago, we have a route for that method:

Route::post('contact', function() {
    //
});

If you press Send on the form now, you will get a blank page. That is because we are not returning anything from our route that handles form submission.

This is the current state of the repository at this moment 315057e9e97d0d34cde9a683cd00bd5e2dedfdff.

Helpers used

We have used two Laravel helpers in this form.

Improve your skills! To learn more about all helpers that come with Laravel visit the documentation for Helpers.

old() - The old function retrieves an old input value flashed into the session. This will be very helpful in our next lessons where we will tackle validation. What this does is it keeps the value that the user entered in the input field so that if the validation fails, the input entered by the user is preserved. He does not need to type it again.

csrf_field() - The csrf_field function generates an HTML hidden input field containing the value of the CSRF token. Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application. Read more about this here.

A quick recap of this lesson:

  • I've shown you how to build a contact form
  • You have learned about old and crsf_field helpers
  • You have been given a lot of documentation to read in order to improve your skills
  • The form can be submitted and we are returned a blank screen

What we are still missing is Validation. Are we going to blindly believe our users, that they have entered a valid email address or that they have entered all fields that we require? Hell no! This is where Laravel shines, the Validation. Stay tuned!

Credits

  • Cover image A Moustache's Grin created by SherifB

Comments