Skip to content

Instantly share code, notes, and snippets.

@lotsofbytes
Last active July 21, 2018 19:03
Show Gist options
  • Save lotsofbytes/83c0e4780c43a3d34b635608b0bcb732 to your computer and use it in GitHub Desktop.
Save lotsofbytes/83c0e4780c43a3d34b635608b0bcb732 to your computer and use it in GitHub Desktop.
[バリデーション:distinct] #laravel #L55 #validation

バリデーション:distinct

入力値(配列である)には、重複の値がないことを判定。

laravel 5.5

gistの画面

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

$ git clone git@gist.github.com:83c0e4780c43a3d34b635608b0bcb732.git tests/Unit/Validations/Distinct
<?php
namespace Tests\Unit\Validations\Distinct;
use Tests\TestCase;
use Validator;
class DistinctTest extends TestCase
{
/**
* @test
* @dataProvider provider_distinct
*/
public function distinct($input, $expected)
{
$v = Validator::make(
$input,
['field.*' => 'distinct'] // fieldだけではエラー。*が必要。
);
$this->assertEquals($expected, $v->passes());
}
public function provider_distinct()
{
return [
[['field' => null], true],
[['field' => ''], true],
[['field' => ' '], true], // space
[['field' => ['犬', '猫']], true],
[['field' => ['犬', '猫', '犬']], false]
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment