From 4cf5eb9307f09b51cdd0d075be07de9163c07eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Silva?= Date: Wed, 16 Jan 2019 17:36:45 +0000 Subject: [PATCH 1/2] Only validate choice field if is not empty and if is not a valid choice --- src/Fields/ChoiceField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fields/ChoiceField.php b/src/Fields/ChoiceField.php index f17581d..7313139 100644 --- a/src/Fields/ChoiceField.php +++ b/src/Fields/ChoiceField.php @@ -41,7 +41,7 @@ public function validate($value) { parent::validate($value); - if (!$this->validValue($value)) { + if (!$this->isEmpty($value) && !$this->validValue($value)) { if (is_array($value)) { $value_diff = array_diff($value, array_keys($this->choices)); $value = implode(', ', $value_diff); From 6f534459c50186e7d858167bc8b619527b42a4c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Silva?= Date: Fri, 18 Jan 2019 16:18:14 +0000 Subject: [PATCH 2/2] Fix tests --- composer.json | 2 +- tests/unit/Fields/ChoiceFieldTest.php | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 1f960dc..3474a76 100644 --- a/composer.json +++ b/composer.json @@ -23,8 +23,8 @@ "twig/twig": ">=1.35" }, "require-dev": { - "phpunit/phpunit": "~6.0", "php-coveralls/php-coveralls": "^1.0", + "phpunit/phpunit": "^6.5", "squizlabs/php_codesniffer": "*" }, "autoload": { diff --git a/tests/unit/Fields/ChoiceFieldTest.php b/tests/unit/Fields/ChoiceFieldTest.php index 4fa6fcb..7ac4bf4 100644 --- a/tests/unit/Fields/ChoiceFieldTest.php +++ b/tests/unit/Fields/ChoiceFieldTest.php @@ -61,16 +61,6 @@ public function testValidateChoiceUnexistent() $this->field->validate("option2"); } - /** - * @expectedException PHPForm\Exceptions\ValidationError - * @expectedExceptionMessage Select a valid choice. "empty string" is not one of the available choices. - */ - public function testValidateChoiceEmpty() - { - $field = new ChoiceField(["choices" => array("option1" => "Option1")]); - $field->validate(""); - } - public function testValidateValidValue() { $this->assertNull($this->field->validate("option1"));