Skip to content

Instantly share code, notes, and snippets.

@lotsofbytes
Last active September 1, 2018 20:23
Show Gist options
  • Save lotsofbytes/3228d9f39b1863976cd5298e2c19408e to your computer and use it in GitHub Desktop.
Save lotsofbytes/3228d9f39b1863976cd5298e2c19408e to your computer and use it in GitHub Desktop.
[バリデーション:present] #laravel #L55 #validation

バリデーション:present

指定の項目があるかどうか判定。項目の値はどの値でもOK。

laravel 5.5

gistの画面

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

$ git clone git@gist.github.com:3228d9f39b1863976cd5298e2c19408e.git tests/Unit/Validations/Present
<?php
namespace Tests\Unit\Validations\Present;
use Tests\TestCase;
use Validator;
class PresentTest extends TestCase
{
/**
* @test
* @dataProvider provider_present
*/
public function present($input, $expected)
{
$v = Validator::make(
$input,
['field' => 'present|string']
);
$this->assertEquals($expected, $v->passes());
}
public function provider_present()
{
return [
// 'field'の項目がある
[['field' => null], false],
[['field' => ''], true],
[['field' => ' '], true], // space
[['field' => '値がある'], true],
[['field' => '1234'], true],
[['field' => 1234], false],
// 'field'の項目はない
[['field2' => '値がある'], false],
];
}
/**
* @test
* @dataProvider provider_without_present
*/
public function without_present($input, $expected)
{
$v = Validator::make(
$input,
['field' => 'string']
);
$this->assertEquals($expected, $v->passes());
}
public function provider_without_present()
{
return [
// 'field'の項目がある
[['field' => null], false],
[['field' => ''], true],
[['field' => ' '], true], // space
[['field' => '値がある'], true],
[['field' => '1234'], true],
[['field' => 1234], false],
// 'field'の項目はない
[['field2' => '値がある'], true], // ここが上と違う
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment