Skip to content

Instantly share code, notes, and snippets.

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

バリデーション:numeric

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

laravel 5.5

gistの画面

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

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