Skip to content

Instantly share code, notes, and snippets.

@lotsofbytes
Last active October 6, 2018 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lotsofbytes/09e606edbaed91e3be3b19ef9d23b21d to your computer and use it in GitHub Desktop.
Save lotsofbytes/09e606edbaed91e3be3b19ef9d23b21d to your computer and use it in GitHub Desktop.
[バリデーション:url] #laravel #L55 #validation

バリデーション:url

入力値が正しいURLの文字列かを判定。URLが存在するかどうかの判定ではない。

laravel 5.5

gistの画面

以下の実行で、ダウンロードできます

$ git clone git@gist.github.com:09e606edbaed91e3be3b19ef9d23b21d.git tests/Unit/Validations/Url
<?php
namespace Tests\Unit\Validations\Url;
use Tests\TestCase;
use Validator;
class UrlTest extends TestCase
{
/**
* @test
* @dataProvider provider_url
*/
public function url($input, $expected)
{
$v = Validator::make(
$input,
['field' => 'required|url']
);
$this->assertEquals($expected, $v->passes());
}
public function provider_url()
{
return [
[['field' => 'https://larajapan.com'], true],
[['field' => 'https://www.larajapan.com'], true],
[['field' => 'https://larajapan.com?foo=bar'], true],
[['field' => 'https://fake.larajapan.com'], true],
[['field' => 'larajapan.com'], false], // httpsがない
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment