How to check YouTube video exist Laravel validation
Laravel provides numbers of validation rules. It will also allow us to create some of your own validation rules. One method of registering custom validation rules is using rule objects. To generate a new rule object using make:rule Artisan command. Laravel will kept the new rule in the app/Rules directory. If this directory does not exist, the make:rule Artisan command will create it when you execute to create your rule.
Here I'm going to create a custom rule to check youtube video is valid or not in laravel because there no predefined validation. In this example I will show laravel 8 custom validation rules.
Laravel provides numbers of validation rules. It will also allow us to create some of your own validation rules. One method of registering custom validation rules is using rule objects. To generate a new rule object using make:rule Artisan command. Laravel will kept the new rule in the app/Rules directory. If this directory does not exist, the make:rule Artisan command will create it when you execute to create your rule.
Here I'm going to create a custom rule to check youtube video is valid or not in laravel because there no predefined validation. In this example I will show laravel 8 custom validation rules.
Step 1: Open project folder in git bash and enter command `php artisan make:rule CheckYoutubeID`.
Step 2: Open file app/Rules/CheckYoutubeID.php
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class CheckYoutubeID implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$url = 'https://img.youtube.com/vi/'.$value.'/0.jpg';
if (@file_get_contents($url, 0, NULL, 0, 1)) {
return true;
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Video URL is not valid.';
}
}
Step 3: Enter command `php artisan make:controller PhpEspertoController` to make a controller.
Step 4: Paste below code in laravel\app\Http\Controllers\PhpEspertoController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Rules\CheckYoutubeID;
class PhpEspertoController extends Controller
{
public function phpEsperto(){
return view('welcome');
}
public function phpEspertoSubmit(Request $request){
$rules = [
'youtube_id' => ['required','string','max:255',new CheckYoutubeID()]
];
$customMessages = [
'required' => 'Please enter :attribute.',
'string' => 'Please enter valid :attribute.',
'max' => 'Reduce :attribute length.',
];
$this->validate($request, $rules, $customMessages);
return redirect()->back()->with('message', ' Video ID: '.$request->youtube_id.' is valid');
}
}
Step 5: Create view file welecome.blade.php in laravel/view and paste below code in it`.
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<body class="container">
<div class="jumbotron">
<form action="{{ route('youtubeValidation') }}" method="POST">
@csrf
<div class="form-group">
<label>Youtube Video ID:</label>
<input type="text" name="youtube_id" class="form-control" value="{{ old('youtube_id') }}">
@error('youtube_id')
<strong style="color:red">{{ $message }}</strong>
@enderror
@if(session()->has('message'))
<strong style="color:green">{{ session()->get('message') }}</strong>
@endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-warning">Submit</button>
</div>
</form>
</div>
</div>
</body>
</html>
Step 6: Open routes/web.php and add below routes in it.
Route::get('/youtube-validation', 'App\Http\Controllers\PhpEspertoController@phpEsperto');
Route::post('/youtube-validation/submit', 'App\Http\Controllers\PhpEspertoController@phpEspertoSubmit')->name('youtubeValidation');
Recommanded Articles
- How to create a multilevel category and subcategory in Laravel
- How to check YouTube video exist Laravel validation
- Multiple user roles authentication Laravel 8
- Deploy Laravel project from local to production server
- Make custom pagination URL in Laravel without query strings
- Web Scraping in Laravel using Goutte
- Insert values during migration run laravel
- Validation for string characters only with custom message in Laravel
- Add new columns in a table Laravel
- How to create foreign key constraints in Laravel
Latest Comments