diff --git a/plugins/baser-core/src/Model/Validation/BcValidation.php b/plugins/baser-core/src/Model/Validation/BcValidation.php index 8e471daa86..f32e673a4c 100644 --- a/plugins/baser-core/src/Model/Validation/BcValidation.php +++ b/plugins/baser-core/src/Model/Validation/BcValidation.php @@ -583,4 +583,20 @@ public static function reserved($value): bool return true; } + /** + * 選択リストに同じ項目を複数登録するかをチェック + * + * @param $value + * @return bool + * @checked + * @notodo + * @unitTest + */ + public static function checkSelectList($value): bool + { + $data = preg_split("/\r\n|\n|\r/", $value); + $result = max(array_count_values($data)); + return ($result < 2); + } + } diff --git a/plugins/baser-core/tests/TestCase/Model/Validation/BcValidationTest.php b/plugins/baser-core/tests/TestCase/Model/Validation/BcValidationTest.php index 8f33070ca2..f41450c54d 100644 --- a/plugins/baser-core/tests/TestCase/Model/Validation/BcValidationTest.php +++ b/plugins/baser-core/tests/TestCase/Model/Validation/BcValidationTest.php @@ -584,4 +584,20 @@ public function testCheckHiragana() $this->assertEquals($result, false); } + /** + * test checkSelectList + */ + public function test_checkSelectList() + { + //戻り=falseケース + $str = "あ\rべ\r\nあ\nべ\ntest"; + $result = $this->BcValidation->checkSelectList($str); + $this->assertFalse($result); + //戻り=trueケース + $str = "あa\nべ\nあ"; + $result = $this->BcValidation->checkSelectList($str); + $this->assertTrue($result); + } + + } diff --git a/plugins/bc-custom-content/src/Model/Table/CustomFieldsTable.php b/plugins/bc-custom-content/src/Model/Table/CustomFieldsTable.php index 2cb17867d8..c7bdbdc684 100644 --- a/plugins/bc-custom-content/src/Model/Table/CustomFieldsTable.php +++ b/plugins/bc-custom-content/src/Model/Table/CustomFieldsTable.php @@ -72,6 +72,14 @@ public function validationDefault(Validator $validator): Validator $validator ->scalar('type') ->notEmptyString('type', __d('baser_core', 'タイプを入力してください。')); + $validator + ->add('source', [ + 'checkSelectList' => [ + 'provider' => 'bc', + 'rule' => ['checkSelectList'], + 'message' => __d('baser_core', '選択リストに同じ項目を複数登録できません。') + ] + ]); return $validator; }