Youtube link validation rule in laravel; In this post, I will teach you how to validate the youtube link in your form input validation in laravel.
I will make a ruling class. This class will validate the youtube link from the request. This class I will call to validation.
For validation youtube link I will take the help of a regex pattern, I have made an I regex pattern for the youtube link identify, and with Then help of preg_match() PHP function I will match form request youtube link with my regex pattern.
If the preg_match() will check with link to regex patter and matched then it will return true else false. If false means the youtube validation link is invalid. If true then will validation will be false and access next process.
So let's start the valid youtube link validation rule integrate into laravel application with a practical example.
Create Rule Class
Create a ruling class using the below-suggested command in your application. Goto your project and open the terminal just paste this command in your terminal and hit enter.
The "YoutubeValidLink" rule class will create this command in "app/Rules/YoutubeValidLink.php".
php artisan make:rule YoutubeValidLink
Add Rule Validation
Once created rule class in "app/Rules/YoutubeValidLink.php" open the file. We will add `regex_pattern` for youtube link validation in the `passes` method.
In the message method, we will add our custom validation message for the validation response message. The preg_match() PHP function will validate the youtube link with the given regex pattern.
app/Rules/YoutubeValidLink.php<?php namespace App\Rules\APIV1; use Illuminate\Contracts\Validation\Rule; class YoutubeValidLink 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) { $regex_pattern = '/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=|\?v=)([^#\&\?]*).*/'; $match; return preg_match($regex_pattern, $value, $match); } /** * Get the validation error message. * * @return string */ public function message() { return 'The :attribute is not a valid URL'; } }
Use In Controller
First import the youtube rule class into the controller. With your form youtube URL attribute add an array with this class new YoutubeValidLink.
app/Http/Controllers/HomeController.php<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\User; use App\Rules\YoutubeValidLink; // IMPORT RULE CLASS class HomeController extends Controller { public function store(Request $request) { $validated = $this->validate($request,[ 'youtube_link' => ['required',new YoutubeValidLink] ]); dd($validated); } }
That's it, If the validation will false, It will return with the given message to the back.
We always thanks to you for reading our blogs.

Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Laravel 6 call function undefined str_slug() - fix
- Bootstrap 5 Modal Not Working Blade File Laravel
- Laravel 9 Dropzone Multiple File Upload Example
- What are accessors and mutators In Laravel
- PhonePe Payment Gateway Integration in Laravel
- Facade Laravel
- Laravel 9 Ajax Image Upload With Preview Tutorial
- How To File Upload AWS s3 Bucket In Laravel 9
- HTTP Client Guzzle Package With Basic Usage In Laravel
- How To Get Current Request Method Post Patch Get In Laravel - PHP