form-validation-in-laravel-img

Form validation in laravel

Hello World, Welcome to my website. Today I am going to create article about how we can perform form validation in laravel. In the previous article we have discussed that how we can generate dummy data in laravel. In this article, we will create student detail form and validate that form in laravel feature. I have created this article in following steps:

  • Create Student form template
  • Student controller and route setting
  • Show the error in the template

I have assumed that you have installed laravel in your server. If not then please follow this article.

Create Student form template

In this step, we will create form for submitting student data.

  • Create resource/add-student.blade.php
  • Add the following code in this file.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Add Student Data</title>
    {{-- Bootstrap 4 --}}
    <link rel="stylesheet" href="{{ asset('lib/bootstrap.min.css') }}">
</head>
<body>
    <div class="container mt-4">
        <div class="row">
            <div class="col-8 offset-2">
                <a href="http://projectsplaza.com"><img src="{{ asset('imgs/projectsplazadotcom.png') }}" /></a>
                <div class="card mt-3">
                    <div class="card-header">Add Student Data</div>
                    <div class="card-body">
                        <form method="post" action="{{ url('add-student') }}">
                        {{ csrf_field() }}
                        <table class="table table-bordered">
                            <tr>
                                <th>Name</th>
                                <td><input placeholder="Name" type="text" class="form-control" name="name" />

                                </td>
                            </tr>
                            <tr>
                                <th>Email</th>
                                <td><input placeholder="Email" type="email" class="form-control" name="email" />
                                </td>
                            </tr>
                            <tr>
                                <th>Phone</th>
                                <td><input placeholder="Phone" type="text" class="form-control" name="phone" />
                                </td>
                            </tr>
                            <tr>
                                <th>Adrress</th>
                                <td><textarea placeholder="Address" class="form-control" name="address"></textarea>
                                </td>
                            </tr>
                            <tr>
                                <th>Gender</th>
                                <td>
                                    <input type="radio" name="gender" value="male" /> Male
                                    <input type="radio" name="gender" value="female" /> Female
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <input type="submit" class="btn btn-danger" />
                                </td>
                            </tr>
                        </table>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Student Controller and route setting

In this step we will add student form show method and submit form methods in the student controller. We will also set the route for these methods. For Student controller, please refer this article.

  • Add following two methods in the student controller.
  • // Student Detail Form
        function add_student(){
            return view('add-student');
        }
        // Add Data
        function submit_student(Request $request){
            $request->validate([
                'name'=>'required',
                'email'=>'required|email',
                'phone'=>'required',
                'address'=>'required',
                'gender'=>'required'
            ]);
            return redirect('add-student');
        }

     

  • Add following routes in the /routes/web.php
  • Route::get('add-student','StudentController@add_student');
    Route::post('add-student','StudentController@submit_student');

     

Show the error in the template

In this step, we will show error message for individual form field.

  • Add the following code in the /resources/add-student.blade.php file after every form field and replace the field name according to field. In the following code i have add error code for name only.
@if($errors->has('name'))
                                    @foreach($errors->get('name') as $message)
                                    <p class="p-0 text-danger">{{ $message }}</p>
                                    @endforeach
                                @endif

I hope you will like this article :). Please subscribe for latest updates. If you have any queries then please send your message in comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *