Laravel 5.6 Server Side Validation Example

Today, We want to share with you Laravel 5.6 Server Side Validation Example Using Resource Controller.In this post we will show you server side api validation in laravel 5.7, hear for Laravel 5.7 Validation With Custom Rules Example From Scratch we will give you demo and example for implement.In this post, we will learn about Request based form validation with Laravel 5.7 with an example.

Laravel 5.6 Server Side Validation Example Using Resource Controller

There are the Following The simple About Laravel 5.6 Server Side Validation Example Using Resource Controller Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel Form Validation- Server Side form validation, so the some Server side Validation for this example is following below.

Laravel 5.7 Server side Validation Example

resources/views/product/create.blade.php

Simple Create View Blade file For Server Side Laravel validation in Resource Controller

app/Http/Controllers/ProductController.php

public function store()
    {
        $rules = array(
            'name' => 'required|string|max:255',
            'price' => 'required|is_integer()',
            'qty' => 'required|is_integer()'
        );
        $params = $request->all();
        $validator = Validator::make($params, $rules);
        if ($validator->fails()) {
            $request->merge(array('add_product_form' => 1));
            //print_r($request->all());die('jjj');
            $input['add_product_form'] = '1';
            return redirect('product.create')
                        ->withErrors($validator)
                        ->withInput();
        } else {

            Product::create($request->all());

            flash()->success('The product has been created successfully.');

            return redirect()->route('product.index');  
        }
        
    }

Example 2 : Laravel Form Validation- Server Side form validation

Defining Routes

Route::get('sell', function(){
    return view('sell');
});
Route::post('sell', 'ProductController@sell');

Creating Sell Form




Get Product Online



Fill the form to sell

Creating the Controller

name = $req->name;
  $Product->product_code= $req->product_code;
  $Product->productminmax = $req->productminmax;
  $Product->producttype = $req->producttype;
  $Product->promo_code = $req->promo_code;

  $Product->save();

  echo "Product Created Successfully";

 }
}

Simple Laravel Validation Logic

validate($req, [
   'name' => 'required',
   'product_code' => 'required|product_code',
   'productminmax' => 'required|max:10|min:10',
   'producttype' => 'required',
   'promo_code' => 'required|confirmed',
   'promo_code_confirmation' => 'required',
  ]);
  $Product = new \App\Product;
  $Product->name = $req->name;
  $Product->product_code= $req->product_code;
  $Product->productminmax = $req->productminmax;
  $Product->producttype = $req->producttype;
  $Product->promo_code = $req->promo_code;

  $Product->save();

  echo "Product Created Successfully";

 }
}

Displaying All the Errors

@if (count($errors) > 0)
 
    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif

Laravel Server side Form Validation- using AJAX Request and Validation

$("#form-id").submit(function(e){
 e.preventDefault();
 $.ajax({
  url: "Your_api_url",
  type: "POST",
  data: new FormData(this),
  contentType: false,
  cache: false,
  processData:false,
  success: function(data){
   //(Good Luck)do something on success
   //append div for success                       
  },
  error: function(data){
   var errors = data.responseJSON;
   for(i in errors){
    $("#div-id").append('

'+errors[i]+'

'); } } }); });
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel 5.6 Server Side Validation Example Using Resource Controller.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment