Skip to content

Instantly share code, notes, and snippets.

@lotsofbytes
Last active October 6, 2018 19:26
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/ac695d75dc1ff830aaebb091335dad15 to your computer and use it in GitHub Desktop.
Save lotsofbytes/ac695d75dc1ff830aaebb091335dad15 to your computer and use it in GitHub Desktop.
[バリデーション:active_url] #laravel #L55 #validation

バリデーション:active_url

入力値が存在するURLの文字列かを判定。

laravel 5.5

gistの画面

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

$ git clone git@gist.github.com:ac695d75dc1ff830aaebb091335dad15.git tests/Unit/Validations/ActiveUrl
<?php
namespace Tests\Unit\Validations\ActiveUrl;
use Tests\TestCase;
use Validator;
class ActiveUrlTest extends TestCase
{
/**
* @test
* @dataProvider provider_active_url
*/
public function active_url($input, $expected)
{
$v = Validator::make(
$input,
['field' => 'required|string|active_url']
);
$this->assertEquals($expected, $v->passes());
}
public function provider_active_url()
{
return [
[['field' => 'https://larajapan.com'], true],
[['field' => 'https://www.larajapan.com'], true],
[['field' => 'https://larajapan.com?foo=bar'], true],
[['field' => 'larajapan.com'], false], // httpsがない
[['field' => 'https://fake.larajapan.com'], false] // 存在しないドメイン名。プロバイダーによってはtrueとなる場合あり
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment