How to use form validation in Laravel 5

In this article laravel 5 form validation in a simple way. A form validation means us forcing user to enter correct data in form field according to our need. Laravel 5 provide a simple, convenient facility for validating data and retrieving validation error messages using Validation class and using request.

In below we are making a fully code to validate a form in laravel using controller.

Making view files which contain form and form for fields and validation error message showing code. In controller we are validating all input and applying validation rules and custom error messages and sending back to view with validation error messages on view.

Let’s create a form to understand how to use form validation in laravel 5.

Route to controller

Route::get('add-post', [
    'as' => 'add-post',
    'uses' => 'PostController@addPost'
]);

Controller to view

public function addPost()
{
   return view('addpost');
}

Create a view file named “addpost.blade.php” under “resources/views/addpost.blade.php” and add below code.

<form enctype="multipart/form-data" method="post" action="{{url('InsertPost')}}">
      <input type="hidden" name="_token" value="{{ csrf_token() }}">
    
    <div class="form-group">
      <label>Title<span class="required">*</span>
      </label>
      <div class="col-md-12 col-sm-12 col-xs-12">
        <input type="text" name="title" value="{{ old('title') }}"  class="form-control">
        <div style="color:#FF0000;">{{$errors->first('title')}}</div>
      </div>
    </div>

    <div class="form-group">
      <label>Slug<span class="required">*</span>
      </label>
      <div class="col-md-12 col-sm-12 col-xs-12">
        <input type="text" name="slug" class="form-control" value="{{ old('slug') }}">
        <div style="color:#FF0000;"> {{$errors->first('slug')}}</div>
      </div>
    </div>

    <div class="form-group">
      <label>Meta Description<span class="required">*</span>
      </label>
      <div class="col-md-12 col-sm-12 col-xs-12">
        <input type="text" name="meta_description" class="form-control" value="{{ old('meta_description') }}">
        <div style="color:#FF0000;"> {{$errors->first('meta_description')}}</div>
      </div>
    </div>

    <div class="form-group">
      <label>Body<span class="required">*</span>
      </label>
      <div class="col-md-12 col-sm-12 col-xs-12">
        <textarea  rows="20" class="form-control" name="body">{{ old('body') }}</textarea>
        <div style="color:#FF0000;"> {{$errors->first('body')}}</div>
      </div>
    </div>

    <d v class="ln_solid"></div>
    <div class="form-group">
      <div class="col-md-12 col-sm-12 col-xs-12">
        <button type="submit" class="btn btn-success">Submit</button>
      </div>
    </div>
</form>

View to Route (post form data)

Route::post('InsertPost', [
    'as' => 'InsertPost',
    'uses' => 'PostController@InsertPost'
]);

Route to controller

public function InsertPost(Request $request)
{

    $this->validate($request, array(
        'title' => 'required|max:255',
        'slug' => 'required|alpha_dash|min:5|max:255|unique:tbl_add_post,slug',
        'meta_description' => 'required|min:1|max:500',
        'body' => 'required'
    ));

   
    $post = new PostModel;
    $post->title =  $request->title;
    $post->slug =  $request->slug;
    $post->meta_description =  $request->meta_description;
    $post->body =  $request->body;
 	$post->save();
 	return redirect('/admin/post/view-post')->with('success','Post added Successfully!');
}

Now, if validation fails, the request will be redirected to back URL and generate error to form and old value display on form so, finally completed form validation in laravel.

Leave a Reply

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