YouTube Icon

Code Playground.

How to use form validation in Laravel 5

CFG

How to use form validation in Laravel 5

In this article, the validation of laravel 5 forms is simple. A validation of the form means that we force the user to enter correct data in the form field as needed. Laravel 5 provides a simple, easy facility to validate data and to retrieve error messages of validation using Validation class and request.

Below we use the controller to create a full code to validate a type in laravel.

Make view files containing fields type and form and message of error of validation showing code. We validate all inputs and apply validation rules and custom error messages in the controller and send them back to view with error messages of validation on view.

Let's construct a form in laravel 5 to understand how to use form validation.

Manager route

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!');
}




CFG