Use "not required" in Symfony3 when building forms with FosrestBundle
<p>I'm building an API with FosrestBundle and Symfony3. In some part of my work, I've to build forms, and I want some fields of my form set to "not required". But I got errors when I tried creating them.</p>

<p>Here is how I created a form for one of my entity : "Question".</p>

<p>Firstly I created a QuestionType class where I set a required to false:</p>

<pre><code>class QuestionType extends AbstractType{

public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder-&gt;add('libelle');
    $builder-&gt;add('description');
    $builder-&gt;add('imageRoot');
    $builder-&gt;add('page', 'integer', array('required' =&gt; false));
    $builder-&gt;add('ligne', 'integer', array('required' =&gt; false));

}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver-&gt;setDefaults([
        'data_class' =&gt; 'AppBundle\Entity\Question',
        'csrf_protection' =&gt; false
    ]);
}
}
</code></pre>

<p>Secondly, I configured validation parameters in my validation.yml file :</p>

<pre><code>AppBundle\Entity\Question:
properties:
    libelle:
        - NotBlank: ~
        - Type: string
    description:
        - NotBlank: ~
        - Type: string
    imageRoot:
        - Type: string
    page:
        - Type: integer
        - GreaterThan:
            value: 0
        - LessThanOrEqual:
            value: 1000

    ligne:
        - Type: integer
        - GreaterThan:
            value: 0
        - LessThanOrEqual:
            value: 1000
</code></pre>

<p>Thirdly, I created my form in a controller</p>

<pre><code>class ContenuController extends Controller{

/**
 * @Rest\View(serializerGroups={"contenu"}, statusCode=Response::HTTP_CREATED)
 * @Rest\Post("/lectureContenu/{id}/Questions")
 */
public function postQuestionAction(Request $request)
{
    $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
    $contenu = $em-&gt;getRepository('AppBundle:Contenu')-&gt;find($request-&gt;get('id'));
    $typeQuestion = $em-&gt;getRepository('AppBundle:TypeQuestion')-&gt;find(1);

    if (empty($contenu)) {
        return new JsonResponse(['message' =&gt; 'Contenu de la question inexistant'], Response::HTTP_NOT_FOUND);
    }

    $question = new Question();
    $question-&gt;setContenu($contenu)
             -&gt;setTypeQuestion($typeQuestion);

    $form = $this-&gt;createForm(QuestionType::class, $question);
    $form-&gt;submit($request-&gt;request-&gt;all()); // Validation des données

    if ($form-&gt;isValid()) {
        $em-&gt;persist($question);
        $em-&gt;flush();

        return $question;
    } else {
        return $form;
    }

  }
 }
</code></pre>

<p>Finally, when I run my code with the example below (using Postman) :</p>

<pre><code> {
  "libelle": "test",
  "description": "test",
  "imageRoot": "test"
 }
</code></pre>

<p>I got this error :</p>

<pre><code>...
"message": "Could not load type \"integer\"",
"class": "Symfony\\Component\\Form\\Exception\\InvalidArgumentException",
...
</code></pre>

<p>I don't know why ! For me, everything seems correct. Please help !</p>