Form Handling in Laravel: Getting Started


Forms are a crucial part of web applications, allowing users to input and submit data. Laravel provides powerful tools for handling forms effortlessly. In this guide, we'll walk you through the basics of form handling in Laravel, from creating forms to processing user input.


Creating Forms


In Laravel, you can create forms using Blade, the built-in templating engine. Blade provides a clean and expressive way to generate HTML forms. To create a basic form, use the `form` and `csrf` directives to open a form and include a CSRF token for security:


        
<form method="POST" action="/process">
@csrf
<!-- Form fields go here -->
<button type="submit">Submit</button>
</form>

This code creates a form that will submit data to the `/process` route when the user clicks the "Submit" button.


Handling Form Submissions


In Laravel, you can handle form submissions by defining a route and a controller method to process the submitted data. For example, you can define a route like this in your `web.php`:


        
Route::post('/process', 'FormController@process');

Then, create a `FormController` with a `process` method to handle the form submission:


        
public function process(Request $request) {
// Process the submitted data
$data = $request->all();
// Perform actions, such as saving data to a database
// Redirect the user or display a success message
}

The `Request` object provides access to the submitted form data, which you can use to perform actions like saving data to a database or performing business logic.


Validation


Form validation is essential to ensure that the submitted data is accurate and secure. Laravel provides a robust validation system. You can define validation rules in your controller method like this:


        
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users',
'password' => 'required|min:8',
]);

If the submitted data does not meet the defined validation rules, Laravel will automatically return an error response and redirect the user back to the form with error messages.

Displaying Errors


To display validation errors, you can use Blade templates. Laravel's `@error` directive helps you display error messages next to form fields:


        
<input type="text" name="name" value="{{ old('name') }}">
@error('name')
<div class="alert alert-danger">{{ $message }}</div>
@enderror

The `old()` function is used to populate the form fields with the previously submitted data. If validation fails, Laravel will automatically redirect the user back to the form with the error messages.


Conclusion


Form handling in Laravel is an essential skill for web developers. With Laravel's powerful form handling capabilities, you can easily create and process forms, validate user input, and provide a smooth user experience. As you become more proficient with Laravel, you can explore advanced features like form requests, custom validation rules, and handling file uploads.


For further learning, consult the official Laravel documentation and explore practical tutorials and examples related to form handling in Laravel web development.