Skip to content

Instantly share code, notes, and snippets.

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

バリデーション:min

入力値が、指定のサイズ以上の値であることを判定。入力値が、文字列なら文字数、配列ならその個数がサイズとなる。 文字列を数値としての扱いたいなら、numericあるいはintegerが前ルールとして必要(例:integer|size:4)。

laravel 5.5

gistの画面

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

$ git clone git@gist.github.com:c12f4094c869e47b6ccaa00a5a5197f9.git tests/Unit/Validations/Min
<?php
namespace Tests\Unit\Validations\Min;
use Tests\TestCase;
use Validator;
class MinTest extends TestCase
{
/**
* @test
* @dataProvider provider_min
*/
public function min($input, $expected)
{
$v = Validator::make(
$input,
['field' => 'required|min:3']
);
$this->assertEquals($expected, $v->passes());
}
public function provider_min()
{
return [
// 文字列なら長さ
[['field' => '000'], true],
[['field' => '0000'], true],
[['field' => 'abcd'], true],
[['field' => '00'], false],
// 配列なら個数
[['field' => ['a', 'b', 'c']], true],
[['field' => ['a', 'b', 'c', 'd']], true],
[['field' => ['a', 'b', 'c', 'd', 'e']], true],
[['field' => ['a', 'b']], false],
];
}
/**
* @test
* @dataProvider provider_min_numeric
*/
public function min_numeric($input, $expected)
{
$v = Validator::make(
$input,
['field' => 'required|numeric|min:3'] // numericが必要
);
$this->assertEquals($expected, $v->passes());
}
public function provider_min_numeric()
{
return [
// 数字なら整数
[['field' => '3'], true],
[['field' => '4'], true],
[['field' => '0004'], true],
[['field' => '3.9'], true],
[['field' => '2'], false],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment