Skip to content

Instantly share code, notes, and snippets.

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

バリデーション:filled

指定の項目があるとき、値があるか(空あるいはnull)判定。

laravel 5.5

gistの画面

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

$ git clone git@gist.github.com:5fe09f58a67187274248d05d95cf8e55.git tests/Unit/Validations/Filled
<?php
namespace Tests\Unit\Validations\Filled;
use Tests\TestCase;
use Validator;
class FilledTest extends TestCase
{
/**
* @test
* @dataProvider provider_filled
*/
public function filled($input, $expected)
{
$v = Validator::make(
$input,
['field' => 'filled|string']
);
$this->assertEquals($expected, $v->passes());
}
public function provider_filled()
{
return [
// 'field'の項目がある
[['field' => null], false],
[['field' => ''], false],
[['field' => ' '], false], // space
[['field' => '値がある'], true],
[['field' => '1234'], true],
[['field' => 1234], false],
// 'field'の項目はない
[['field2' => '値がある'], true],
];
}
/**
* @test
* @dataProvider provider_without_filled
*/
public function without_present($input, $expected)
{
$v = Validator::make(
$input,
['field' => 'string']
);
$this->assertEquals($expected, $v->passes());
}
public function provider_without_filled()
{
return [
// 'field'の項目がある
[['field' => null], false],
[['field' => ''], true], // ここが上と違う
[['field' => ' '], true], // ここが上と違う
[['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