Skip to content

Instantly share code, notes, and snippets.

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

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

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

laravel 5.5

gistの画面

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

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