I use Annotation in a Doctrine Entity Class. Annotation for a field are :
/**
* @var integer
*
* @ORM\Column(name="duree", type="integer", nullable=true)
*
* @Form\Type("Zend\Form\Element\Number")
* @Form\Attributes({"required":false, "placeholder":"Durée", "min":"1", "max":"20"})
* @Form\Required(false)
* @Form\AllowEmpty()
* @Form\Options({"label":"Durée :"})
* @Form\Filter({"name": "Int"})
* @Form\Validator({"name":"IsInt"})
*/
private $duree;
So the DB column can be Empty (nullable), and in the form i wan't the same (ie user can leave input empty). I've both annotation Required(false) and allowEmpty, but the form never valid (always got isEmpty for this field).
If i set @Form\Type to "Text", it is working fine (form is valid event if input is empty). But with the class Number, it'is not the same.
I've the same pb with a Select element (correspondinf to a relationship). Annotations are :
/**
* @var \Application\Entity\CcCategorie
*
* @ORM\ManyToOne(targetEntity="Application\Entity\CcCategorie")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="categorie", referencedColumnName="id", nullable=true, onDelete="SET NULL")
* })
*
* @Form\Type("DoctrineModule\Form\Element\ObjectSelect")
* @Form\Attributes({"type":"select", "required":false})
* @Form\Options({"label":"Catégorie :"})
* @Form\Required(false)
* @Form\AllowEmpty()
* @Form\Options({
* "label":"Catégorie :",
* "empty_option": "---",
* "target_class": "Application\Entity\CcCategorie",
* "property": "label"
* })
*/
private $categorie;
But, with this the field has error (isEmpty) if the Select is set to empty option when validating Form.
The only workaround i've found is to set the annotation
* @Form\Type("\Application\Form\Entity\CcRepriseFieldset")
at the top of the entity class. The class CcRepriseFieldset extend Fieldset, and implement InputFilterProviderInterface. Then i specify the function in this class :
public function getInputFilterSpecification()
{
return array(
array(
"name" => "duree",
'required' => false,
'allow_empty' => true,
),
array(
"name" => "categorie",
'required' => false,
'allow_empty' => true,
),
);
}
With this it works... But it's not annotations. I don't understand why annotation not work
thanks
@Form\AllowEmpty(true)I don't use form annotations (same opinion as @Saeven ) but I'd fully expect leaving the parameter empty as you have to be interpreted as afalsey value - Crisp