0

I want to validate the URL with the specified protocol.

HTTP and HTTPS type validation is not applicable for the below URLs.

  1. If the URL is secure the see below.

    rtmps://username:password@server:port/

  2. Without a secured URL will be as follows.

    rtmp://server:port/

I only need validation for the above 2 URL(s) only.

I have tried laravel's URL type validation but it is not validating for these protocols.

Also want to tell you only I am able to use is regex validation, because it is a request class of laravel where I have put the validation rule. laravel version 6.2

Pierre
  • 1,129
  • 2
  • 16
  • 30
MHEMBINT
  • 105
  • 1
  • 10

1 Answers1

0

run this command:

`php artisan make:rule ValidateRTMPUrlRule`

this command will create a custom rule class

`
 <?PHP

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class ValidateRTMPUrlRule 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)
{
   
    $result = parse_url($value);
    $scheme = strtolower($result['scheme']);
    if('rtmp' == $scheme)
    {
        if(isset($result['user']))
        {
            return false;
        }
        if(isset($result['pass']))
        {
            return false;
        }
        return true;
    }
    if('rtmps' == $scheme)
    {
        if(!isset($result['user']))
        {
            return false;
        }
        if(!isset($result['pass']))
        {
            return false;
        }
        return true;
    }
    return false;
}

/**
 * Get the validation error message.
 *
 * @return string
 */
public function message()
{
    return ':attribute is not a valid RTM protocol url.';
}

}
`

then in your request

`$data = $request->validate([
        'url' => ['required', new ValidateRTMPUrlRule],
    ]);`
MHEMBINT
  • 105
  • 1
  • 10