Skip to content

Instantly share code, notes, and snippets.

@lotsofbytes
Last active October 6, 2018 18:59
Show Gist options
  • Save lotsofbytes/78477dbc541f19a45da5921402d220cb to your computer and use it in GitHub Desktop.
Save lotsofbytes/78477dbc541f19a45da5921402d220cb to your computer and use it in GitHub Desktop.
[バリデーション:integer] #laravel #L55 #validation

バリデーション:integer

入力値が、整数であることを判定。

laravel 5.5

gistの画面

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

$ git clone git@gist.github.com:78477dbc541f19a45da5921402d220cb.git tests/Unit/Validations/Integer
<?php
namespace Tests\Unit\Validations\Integer;
use Tests\TestCase;
use Validator;
class IntegerTest extends TestCase
{
/**
* @test
* @dataProvider provider_integer
*/
public function integer($input, $expected)
{
$v = Validator::make(
$input,
['quantity' => 'required|integer']
);
$this->assertEquals($expected, $v->passes());
}
public function provider_integer()
{
return [
// 文字列
[['quantity' => '0'], true],
[['quantity' => '1'], true],
[['quantity' => '-1'], true],
[['quantity' => 'abcd'], false],
[['quantity' => '0001'], false], // numericならtrue
[['quantity' => '1.1'], false],
// 文字列でない
[['quantity' => 0], true],
[['quantity' => 1], true],
[['quantity' => -1], true],
[['quantity' => 1.1], false],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment