How to check YouTube video exist Laravel validation

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.

 

Also Read: How to create a multilevel category and subcategory in Laravel
Also Read: Generate Dummy Posts in WordPress

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.

 

Also Read: How to create a multilevel category and subcategory in Laravel
Also Read: Generate Dummy Posts in WordPress

Step 1: Open project folder in git bash and enter command `php artisan make:rule CheckYoutubeID`.

screenshot-1

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.';
    }
}

screenshot-2

Step 3: Enter command `php artisan make:controller PhpEspertoController` to make a controller.

screenshot-3

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

screenshot-4

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>

screenshot-5

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

screenshot-5

screenshot-7

screenshot-8

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Add your comment

Close