Symfony 2.7, form won't submit if field is blank

216 views Asked by At

I have a classic form, with a few 'entity' type fields and one collection type fields. Those aren't causing any issue. When I put data in all the field, except the description field, as I want it to be null or empty, and submit, my form is processed but the new entity not added to the database, as if the description field needed to be field.

Then I'm redirected to the same form with all data entered gone, as if it had been added in the database.

I've checked the field mapping, which is set to nullable :

/**
 * @var string
 *
 * @ORM\Column(name="description_activite", type="text", nullable=true)
 */
private $descriptionActivite;
public function getDescriptionActivite(){return $this->descriptionActivite;}
public function setDescriptionActivite($value){$this->descriptionActivite=$value;return $this;}

And the field description in the formType file :

     ->add('descriptionActivite', 'textarea', array(
            'label' => 'Description',
            'attr' => array(
                'class' => 'form-control',
                // 'required' => false
            )
        ))

I've also checked the database just in case, the field is created as a may be null field, I really don't know where that problem is coming from. Anyone ran into this already? Thanks

1

There are 1 answers

0
brn On

For those who meet the same problem, I solved it by : Checking database if field may be null (was not the issue but would have been later) Checking annotation, see if the field is set as nullable AND LAST

     ->add('descriptionActivite', 'textarea', array(
        'label' => 'Description',
        'required' => false,
        'attr' => array(
            'class' => 'form-control',
      ) 
    ))

the required option was place in the 'attr' array(), when it should not have, my bad.