Skip to content

Instantly share code, notes, and snippets.

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

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

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

laravel 5.5

gistの画面

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

$ git clone git@gist.github.com:12be776e40115c228e4a227df2797a77.git tests/Unit/Validations/RequiredWith
<?php
namespace Tests\Unit\Validations\RequiredWith;
use Tests\TestCase;
use Validator;
class RequiredWithTest extends TestCase
{
/**
* @test
* @dataProvider provider_required_with
*/
public function required_with($input, $expected)
{
$v = Validator::make(
$input,
[
'field' => 'required_with:field1,field2'
]
);
$this->assertEquals($expected, $v->passes());
}
public function provider_required_with()
{
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' => '値'], false],
[['field' => '', 'field1' => '値'], false],
[['field' => ' ', 'field1' => '値'], false], // space
[['field' => '値', 'field1' => '値'], true],
// 'field2'の項目があり値がある
[['field' => null, 'field2' => '値'], false],
[['field' => '', 'field2' => '値'], false],
[['field' => ' ', 'field2' => '値'], false], // 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