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

<form action="{{ route('product.store') }}" id="addproduct" method="POST">
    <input name="_token" type="hidden" value="{{ csrf_token() }}">
    <input type="hidden" name="user_id" value="{{ Auth::id() }}">
    <input type="hidden" name="add_product_form" value="1">
    <div class="modal-body">
        <div class="form-group {{ $errors->has('name') ? ' has-error' : '' }}">
            <label class="control-label">product Name</label>
            <input type="text" class="form-control" name="name" value="{{Request::old('name')}}">
            @if ($errors->has('name'))
                <span class="help-block"><strong>{{ $errors->first('name') }}</strong></span>
            @endif
        </div>
        <div class="form-group {{ $errors->has('price') ? ' has-error' : '' }}">
            <label class="control-label">product price</label>
            <input type="number" class="form-control" name="price" value="{{Request::old('price')}}">
            @if ($errors->has('price'))
                <span class="help-block"><strong>{{ $errors->first('price') }}</strong></span>
            @endif
        </div>
        <div class="form-group {{ $errors->has('qty') ? ' has-error' : '' }}">
            <label class="control-label">product Age</label>
            <input type="number" class="form-control" name="qty" value="{{Request::old('qty')}}">
            @if ($errors->has('qty'))
                <span class="help-block"><strong>{{ $errors->first('qty') }}</strong></span>
            @endif
        </div>
    </div>
    <div class="modal-footer">
        <button class="btn btn-info" type="submit">Add</button>
        <button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
    </div>
</form>

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', '[email protected]');

Creating Sell Form

<!DOCTYPE html>
<html>
<head>
<title>Get Product Online</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<form class="form-horizontal">
<fieldset>

<!-- Form Name -->
<legend>Fill the form to sell</legend>

<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name">Name</label>  
<div class="col-md-4">
<input id="name" name="name" type="text" placeholder="Your Product Full Name" class="form-control input-md">

</div>
</div>

<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="product_code">product_code</label>  
<div class="col-md-4">
<input id="product_code" name="product_code" type="text" placeholder="P0001235" class="form-control input-md">

</div>
</div>

<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="productminmax">productminmax</label>  
<div class="col-md-4">
<input id="productminmax" name="productminmax" type="text" placeholder="+91" class="form-control input-md">

</div>
</div>

<!-- Multiple Radios (inline) -->
<div class="form-group">
<label class="col-md-4 control-label" for="producttype">producttype</label>
<div class="col-md-4"> 
<label class="radio-inline" for="producttype-0">
<input type="radio" name="producttype" id="producttype-0" value="Male">
Male
</label> 
<label class="radio-inline" for="producttype-1">
<input type="radio" name="producttype" id="producttype-1" value="Female">
Female
</label>
</div>
</div>

<!-- promo_code input-->
<div class="form-group">
<label class="col-md-4 control-label" for="promo_code">promo_code</label>
<div class="col-md-4">
<input id="promo_code" name="promo_code" type="text" placeholder="promo_code" class="form-control input-md">

</div>
</div>

<!-- promo_code input-->
<div class="form-group">
<label class="col-md-4 control-label" for="corfirm">Confirm promo_code</label>
<div class="col-md-4">
<input id="corfirm" name="corfirm" type="text" placeholder="Confirm promo_code" class="form-control input-md">

</div>
</div>

<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<input type="submit" class="btn btn-success" value="sell">
</div>
</div>

</fieldset>
</form>

</body>
</html>

Creating the Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class ProductController extends Controller
{
//
 public function sell(Request $req){
  $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";

 }
}

Simple Laravel Validation Logic

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class ProductController extends Controller
{
//
 public function sell(Request $req){

  $this->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)
 <div class="alert alert-danger">
 <ul>
  @foreach ($errors->all() as $error)
   <li>{{ $error }}</li>
  @endforeach
 </ul>
 </div>
@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('<p>'+errors[i]+'</p>');
   }
  }           
 });
});

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read :  AngularJS Live Search Box using PHP MySqli

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 Reply

avatar
  Subscribe  
Notify of