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.
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,