01.Create a controller
1 |
php artisan make:controller CustomValDemoController |
02.Route
1 2 |
Route::get('/', 'CustomValDemoController@customVali'); Route::post('customValiPost', 'CustomValDemoController@customValiPost'); |
03.Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\URL; class CustomValDemoController extends Controller { // public function customVali() { return view('customVali'); } public function customValiPost(Request $request) { $this->validate($request, [ 'title' => 'required', 'picture' => 'required |image ', ]); $avarta = Input::file('picture'); if(strpos($avarta->getClientMimeType(),'image') !== FALSE){ $upload_folder = '/assets/uploads/'; //$file_name = str_random(). '.' . $avarta->getClientOriginalExtension(); $file_name = $avarta->getClientOriginalName(); $avarta->move(public_path() . $upload_folder, $file_name); echo URL::asset($upload_folder . $file_name); // get upload file url } print_r($request->title); } } |
04.and finally the view uploadfile.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<html lang="en"> <head> <title>Custom Validation Rule Laravel 5</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" > </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">Custom Validation Rule Laravel 5</a> </div> </div> </nav> <div class="container"> <form action="{{ URL::to('customValiPost') }}" class="form-horizontal" method="post" enctype="multipart/form-data"> <input name="_token" type="hidden" value="{{csrf_token()}}"/> @if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <input type="text" name="title" class="form-control" style="width:30%" placeholder="Add Odd String" /> @if ($errors->has('title')) <span class="help-block"> <strong>{{ $errors->first('title') }}</strong> </span> @endif <br/> <input type="file" class="form-control" name="picture" id="picture"> <button class="btn btn-primary">Save</button> </form> </div> </body> </html> |
05.Create a custom validation
Go to App\Providers in AppServiceProvider
1 2 3 4 5 6 7 8 9 10 |
use Validator; //use it in top import just below namespace public function boot() { //... \Validator::extend('foo', function($attribute, $value, $parameters, $validator) { return $value=='foo'; }); } |
and write a validation message in resources\lang\en\validation.php
1 |
'foo' => "The :attribute must be even string lenght.", |
cleaner with DI
#in Repositories
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php /** * Created by IntelliJ IDEA. * User: Dell * Date: 1/27/2017 * Time: 11:27 AM */ namespace App\Http\Repositories; use Illuminate\Support\Facades\URL; class CommonRepository{ /** * File save to common directory 'public/assets/uploads/'; * returns file path * @param null $file * @return string */ public function fileUpload($file) { if(strpos($file->getClientMimeType(),'image') !== FALSE){ $upload_folder = '/assets/uploads/'; //$file_name = str_random(). '.' . $avarta->getClientOriginalExtension(); $file_name = $file->getClientOriginalName(); $file->move(public_path() . $upload_folder, $file_name); return URL::asset($upload_folder . $file_name); // get upload file url } return 'file is not image file'; } } |
#controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Repositories\CommonRepository; class CustomValDemoController extends Controller { protected $commonRepository; public function __construct(CommonRepository $commonRepository) //Depedecy injection here .... { $this->commonRepository = $commonRepository; } public function customVali() { return view('customVali'); } public function customValiPost(Request $request) { $this->validate($request, [ 'title' => 'required', 'picture' => 'required |image ', ]); $picture = $request->file('picture'); $inputFilePath=$this->commonRepository->fileUpload($picture); print_r($inputFilePath); } } |
For more information go to
//custom validation
//http://itsolutionstuff.com/post/laravel-5-create-custom-validation-rule-exampleexample.html
//validation
//http://stackoverflow.com/questions/28417977/custom-validator-in-laravel-5