diff --git a/src/Html/FormBuilder.php b/src/Html/FormBuilder.php index a671a558a..b3dbd9e0e 100644 --- a/src/Html/FormBuilder.php +++ b/src/Html/FormBuilder.php @@ -285,6 +285,10 @@ public function input(string $type, ?string $name = null, ?string $value = null, // when creating the HTML element. Then, we will return the entire input. $merge = compact('type', 'value', 'id'); + if ($id === "") { + unset($merge['id']); + } + $options = array_filter(array_merge($options, $merge), function ($item) { return !is_null($item); }); diff --git a/tests/Html/FormBuilderTest.php b/tests/Html/FormBuilderTest.php new file mode 100644 index 000000000..6bee1c4eb --- /dev/null +++ b/tests/Html/FormBuilderTest.php @@ -0,0 +1,58 @@ +formBuilder = new FormBuilder($htmlBuilder, $generator); + } + + public function testInputIdMissing() + { + $result = $this->formBuilder->input(type:"text", name:"my-name", value:"my value"); + $this->assertEquals('', $result); + } + + public function testInputIdEmpty() + { + $result = $this->formBuilder->input(type:"text", name:"my-name", value:"my value", options:["id"=>""]); + $this->assertEquals('', $result); + } + + public function testInputIdNull() + { + $result = $this->formBuilder->input(type:"text", name:"my-name", value:"my value", options:["id"=>null]); + $this->assertEquals('', $result); + } + + public function testInputIdFalse() + { + $result = $this->formBuilder->input(type:"text", name:"my-name", value:"my value", options:["id"=>false]); + $this->assertEquals('', $result); + } + + public function testInputIdZero() + { + $result = $this->formBuilder->input(type:"text", name:"my-name", value:"my value", options:["id"=>0]); + $this->assertEquals('', $result); + } + + public function testInputIdMissingWithAssociatedLabel() + { + $result = $this->formBuilder->label(name:"my-input", value:"my input label"); + $result = $this->formBuilder->input(type:"text", name:"my-input", value:"my value"); + $this->assertEquals('', $result); + } +}