Skip to content

Instantly share code, notes, and snippets.

@lotsofbytes
Last active October 7, 2018 00:53
Show Gist options
  • Save lotsofbytes/272c2ff7ce033c00cbd1949f3e4edb09 to your computer and use it in GitHub Desktop.
Save lotsofbytes/272c2ff7ce033c00cbd1949f3e4edb09 to your computer and use it in GitHub Desktop.
[バリデーション:required_without_all] #laravel #L55 #validation

バリデーション:required_without_all:foo,bar,...

依存するすべての項目(コンマ区切りで複数可:foo,bar,...)がない、あるいは項目に値がないときに、指定の項目があり値がある(空あるいはnullでない)ならOK。

laravel 5.5

gistの画面

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

$ git clone git@gist.github.com:272c2ff7ce033c00cbd1949f3e4edb09.git tests/Unit/Validations/RequiredWithoutAll
<?php
namespace Tests\Unit\Validations\RequiredWithoutAll;
use Tests\TestCase;
use Validator;
class RequiredWithoutAllTest extends TestCase
{
/**
* @test
* @dataProvider provider_required_without_all
*/
public function required_without_all($input, $expected)
{
$v = Validator::make(
$input,
[
'field' => 'required_without_all:field1,field2'
]
);
$this->assertEquals($expected, $v->passes());
}
public function provider_required_without_all()
{
return [
// 'field1'もfield2'も項目がない
[['field' => null], false],
[['field' => ''], false],
[['field' => ' '], false], // space
[['field' => '値'], true],
// 'field1'の項目があるが値がない
[['field' => null, 'field1' => ''], false],
[['field' => '', 'field1' => ''], false],
[['field' => ' ', 'field1' => ''], false], // space
[['field' => '値', 'field1' => ''], true],
// 'field1'の項目があり値がある
[['field' => null, 'field1' => '値'], true],
[['field' => '', 'field1' => '値'], true],
[['field' => ' ', 'field1' => '値'], true], // space
[['field' => '値', 'field1' => '値'], true],
// 'field2'の項目があり値がある
[['field' => null, 'field2' => '値'], true],
[['field' => '', 'field2' => '値'], true],
[['field' => ' ', 'field2' => '値'], true], // space
[['field' => '値', 'field2' => '値'], true],
// 'field1'もfield2'も項目があり値がある
[['field' => null, 'field1' => '値', 'field2' => '値'], true],
[['field' => '', 'field1' => '値', 'field2' => '値'], true],
[['field' => ' ', 'field1' => '値', 'field2' => '値'], true], // space
[['field' => '値', 'field1' => '値', 'field2' => '値'], true],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment