Skip to content

Instantly share code, notes, and snippets.

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

バリデーション:accepted

入力値が、'yes', 'on', '1'の文字列かを判定。規約の同意のチェックボックスなどで使用。

laravel 5.5

gistの画面

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

$ git clone git@gist.github.com:cf781699c05c73d07fdbff8489052ef8.git tests/Unit/Validations/Accepted
<?php
namespace Tests\Unit\Validations\Accepted;
use Tests\TestCase;
use Validator;
class AcceptedTest extends TestCase
{
/**
* @test
* @dataProvider provider_accepted
*/
public function accepted($input, $expected)
{
$v = Validator::make(
$input,
['field' => 'accepted']
);
$this->assertEquals($expected, $v->passes());
}
public function provider_accepted()
{
return [
[['field' => null], false],
[['field' => ''], false],
[['field' => ' '], false], // space
[['field' => 'yes'], true],
[['field' => 'no'], false],
[['field' => 'on'], true],
[['field' => 'off'], false],
[['field' => '1'], true],
[['field' => '0'], false],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment