Laravel

Laravel Beginner’s Guide: Comprehensive Explanation of Input Form Validation Checks [Using Validator]

This time, let’s learn together how to perform validation checks in input forms using Laravel. Validation checks are a crucial process that ensures the data entered by users into forms is appropriate. By performing these checks properly, you can prevent incorrect data from being saved and improve the reliability of your application.

First, Prepare the Input Form View (/register)

First, let’s create a form where users can enter their data. In this example, we will have four input fields: “Name”, “Address”, “Age”, and “Phone Number”. The “Name” and “Phone Number” fields will be required (indicated with a “※” mark). Write the form in HTML and use Laravel’s Blade template feature to display error messages as well.

The basic structure of the HTML is as follows. Don’t forget to include @csrf for security reasons.

    //Input error messages
    @include('common.errors')

        <form enctype="multipart/form-data" action="{{ url('entry') }}" method="POST">
            @csrf
                 
                <div>
                    <label for="namae">Name※</label>
                    <input type="text" name="namae">
                </div>

                <div>
                    <label for="nenrei">Age</label>
                    <input type="text" name="nenrei">
                </div>

                <div>
                    <label for="add">Address</label>
                    <input type="text" name="add">
                </div>

                <div>
                    <label for="tel">Phone Number※</label>
                    <input type="text" name="tel">
                </div>

                <div>
                    <button type="submit">Register</button>
                </div>

        </form>

Implementing Validation Checks: Writing the Controller

Next, we will check the data entered by the user in the form. In Laravel, you can easily implement validation using the Validator facade. Let’s set the following rules:

  • Name (namae): Required, max 255 characters
  • Age (nenrei): Max 3 characters
  • Address (add): Max 255 characters
  • Phone Number (tel): Required, max 20 characters

 
Set these rules using the Validator::make method. If a validation error occurs, the user will be redirected back to the form, and error messages will be displayed.

    public function entry(Request $request) {

        //Validation check
        $validator = Validator::make($request->all(), [
            'namae' => 'required|max:255',	//Required, max 255 characters
            'nenrei' => 'max:3',	//Max 3 characters
            'add' => 'max:255',	//Max 255 characters
            'tel' => 'required|max:20',	//Required, max 20 characters
        ]);

        //In case of validation errors
        if ($validator->fails()) {
                return redirect('/register')	//Redirect to register (input form)
                    ->withInput()
                    ->withErrors($validator);
        }
  
        //Continue with registration process…

    }

Routing Settings: Writing the Route (Page URL)

In Laravel, routes are set to connect URLs with controller actions. Here, we will configure the above form to be displayed at the URL /register.

Route::get('/register','EntryController@register');

Conclusion

Now you have understood the basic flow of input form validation using Laravel. By following these steps, you can develop secure and efficient web applications. It might seem difficult at first, but give it a try!

 
 
*Please use this at your own risk if reusing.