Hi Folks!
I have been trying to get rid of my form classes and use form annotations to create them; however, I stumbled upon a little something…:
Given the following element:
//use statement for completions sake
use Laminas\Form\Annotation as Form;
/**
* @ORM\Entity
* @ORM\Table(name="Test")
*
* @Form\Name("Test")
*/
class Test
{
/**
* @ORM\Column(type="string")
*
* @Form\Type("textarea")
* @Form\Options({"label" : "Description"})
* @Form\Attributes({"class" : "form-control", "placeholder" : "Description"})
* @Form\Required(false)
* @Form\Filter("StringTrim")
* @Form\Filter("StripTags")
* @Form\Validator("StringLength", options={"encoding": "UTF-8", "min": "1", "max": "65000"})
*
* @var string
*/
public $description;
}
which generates a form specification like this:
{
"name": "Test",
"attributes": [],
"elements": [
{
"flags": [],
"spec": {
"name": "description",
"type": "textarea",
"options": { "label": "Description" },
"attributes": { "class": "form-control", "placeholder": "Description" }
}
}
],
"input_filter": {
"description": {
"name": "description",
"required": true,
"filters": [
{ "name": "StringTrim" },
{ "name": "StripTags" },
{ "name": "StringTrim" }
],
"validators": [
{
"name": "StringLength",
"options": { "encoding": "UTF-8", "min": "1", "max": "65000" }
}
]
}
},
"options": { "prefer_form_input_filter": true }
}
the thing that irks me, is:
Apparently, something thinks that the element has to be required, even though I clearly set it to false in the Annotation, because I get a “this element is required”-message when I leave it empty and submit the form.
To top it off, there is this line in the ElementAnnotationListener that sets an attribute called “required” to “required” when the element is required. According to above formSpec, that does also not happen.
Is anyone able to enlighten me on how to set an element to optional properly?