PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 21, 2022

[FIXED] How to validate Youtube Url validateOnly Laravel Livewire

 January 21, 2022     laravel, laravel-7, laravel-livewire, validation     No comments   

Issue

I'm trying to validate that a youtube address is put in the youtube url, I don't want it to be something strict but at least it can be verified that it doesn't put anything. I am trying to customize the validations and I do not discover how they are done, can you please help me.

I would like to implement something like this:

$rx = '~
  ^(?:https?://)?                           # Optional protocol
   (?:www[.])?                              # Optional sub-domain
   (?:youtube[.]com/watch[?]v=|youtu[.]be/) # Mandatory domain name (w/ query string in .com)
   ([^&]{11})                               # Video id of 11 characters as capture group 1
    ~x';

$has_match = preg_match($rx, $url, $matches);

I use real-time validation as follows:

public function updated($field)

    {

        $this->validateOnly($field, [

            'name' => 'required|max:255|min:3',

            'last_name' => 'required|max:255|min:3',

            'password' => 'min:6|required',

            'password_confirmation' => 'same:password',

            'email' => 'unique:App\User,email|required|email',

        ]);

    }

Can you add a custom validation? I would greatly appreciate an example of how it can be done. Thank you for spending time in my consultation.


Solution

Add custom rule:

public function passes($attribute, $value)
    {
        return (bool) preg_match('/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=|\?v=)([^#\&\?]*).*/',$value);
    }

Validate:

'video' => new RuleYoutube,


Answered By - maraet
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing