Skip to content

Instantly share code, notes, and snippets.

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

バリデーション:alpha

入力値が数字や記号文字を含まない文字列かを判定。半角英字の判定ではないことに注意。つまり日本語の文字列はどれもOK

laravel 5.5

gistの画面

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

$ git clone git@gist.github.com:a17165670783b4f22d88551f5e2ee098.git tests/Unit/Validations/Alpha
<?php
namespace Tests\Unit\Validations\Alpha;
use Tests\TestCase;
use Validator;
class AlphaTest extends TestCase
{
/**
* @test
* @dataProvider provider_alpha
*/
public function alpha($input, $expected)
{
$v = Validator::make(
$input,
['field' => 'required|alpha']
);
$this->assertEquals($expected, $v->passes());
}
public function provider_alpha()
{
return [
[['field' => 'abcd'], true],
[['field' => 'ABCD'], true],
[['field' => 'ログイン'], true],
[['field' => 'ろぐいん'], true],
[['field' => '漢字'], true],
[['field' => 'ab cd'], false],
[['field' => '-'], false],
[['field' => '_'], false],
[['field' => '0'], false],
[['field' => '1'], false],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment