Skip to content

Commit 26e0033

Browse files
authored
Merge pull request #28 from dipcode-software/fix/allow-choice-field-be-empty
Validate choice field if is not empty and if is not a valid choice
2 parents 81e0717 + 6f53445 commit 26e0033

File tree

3 files changed

+2
-12
lines changed

3 files changed

+2
-12
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"twig/twig": ">=1.35"
2424
},
2525
"require-dev": {
26-
"phpunit/phpunit": "~6.0",
2726
"php-coveralls/php-coveralls": "^1.0",
27+
"phpunit/phpunit": "^6.5",
2828
"squizlabs/php_codesniffer": "*"
2929
},
3030
"autoload": {

src/Fields/ChoiceField.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function validate($value)
4141
{
4242
parent::validate($value);
4343

44-
if (!$this->validValue($value)) {
44+
if (!$this->isEmpty($value) && !$this->validValue($value)) {
4545
if (is_array($value)) {
4646
$value_diff = array_diff($value, array_keys($this->choices));
4747
$value = implode(', ', $value_diff);

tests/unit/Fields/ChoiceFieldTest.php

-10
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,6 @@ public function testValidateChoiceUnexistent()
6161
$this->field->validate("option2");
6262
}
6363

64-
/**
65-
* @expectedException PHPForm\Exceptions\ValidationError
66-
* @expectedExceptionMessage Select a valid choice. "empty string" is not one of the available choices.
67-
*/
68-
public function testValidateChoiceEmpty()
69-
{
70-
$field = new ChoiceField(["choices" => array("option1" => "Option1")]);
71-
$field->validate("");
72-
}
73-
7464
public function testValidateValidValue()
7565
{
7666
$this->assertNull($this->field->validate("option1"));

0 commit comments

Comments
 (0)