Symfony 2 form field radio required=false?









up vote
4
down vote

favorite
2












This is part of my entity class :



/**
* @var integer
*
* @ORMColumn(name="student", type="integer", nullable=true)
*/
private $student;


This part of my form class :



 $builder
->add('student', 'choice', ['label'=> false,
'expanded' => true,
'choices' => (Array)new StudentEnum(),
])
;


Ad this is output :



<input id="xxxxx_0" type="radio" value="4" required="required" name="xxxxx[student]">
<label class="required" for="xxxxxV_student_0">Nie</label>


...



My problem is that my input tag should not have attribute "required" becouse I have set nullable=true in entity.










share|improve this question

























    up vote
    4
    down vote

    favorite
    2












    This is part of my entity class :



    /**
    * @var integer
    *
    * @ORMColumn(name="student", type="integer", nullable=true)
    */
    private $student;


    This part of my form class :



     $builder
    ->add('student', 'choice', ['label'=> false,
    'expanded' => true,
    'choices' => (Array)new StudentEnum(),
    ])
    ;


    Ad this is output :



    <input id="xxxxx_0" type="radio" value="4" required="required" name="xxxxx[student]">
    <label class="required" for="xxxxxV_student_0">Nie</label>


    ...



    My problem is that my input tag should not have attribute "required" becouse I have set nullable=true in entity.










    share|improve this question























      up vote
      4
      down vote

      favorite
      2









      up vote
      4
      down vote

      favorite
      2






      2





      This is part of my entity class :



      /**
      * @var integer
      *
      * @ORMColumn(name="student", type="integer", nullable=true)
      */
      private $student;


      This part of my form class :



       $builder
      ->add('student', 'choice', ['label'=> false,
      'expanded' => true,
      'choices' => (Array)new StudentEnum(),
      ])
      ;


      Ad this is output :



      <input id="xxxxx_0" type="radio" value="4" required="required" name="xxxxx[student]">
      <label class="required" for="xxxxxV_student_0">Nie</label>


      ...



      My problem is that my input tag should not have attribute "required" becouse I have set nullable=true in entity.










      share|improve this question













      This is part of my entity class :



      /**
      * @var integer
      *
      * @ORMColumn(name="student", type="integer", nullable=true)
      */
      private $student;


      This part of my form class :



       $builder
      ->add('student', 'choice', ['label'=> false,
      'expanded' => true,
      'choices' => (Array)new StudentEnum(),
      ])
      ;


      Ad this is output :



      <input id="xxxxx_0" type="radio" value="4" required="required" name="xxxxx[student]">
      <label class="required" for="xxxxxV_student_0">Nie</label>


      ...



      My problem is that my input tag should not have attribute "required" becouse I have set nullable=true in entity.







      forms symfony doctrine2 choice






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 4 '13 at 10:46









      user2156980

      2901314




      2901314






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          17
          down vote



          accepted










          The solution is required => false and empty_value => false



          $builder
          ->add('student', 'choice', [
          'label'=> false,
          'expanded' => true,
          'choices' => (Array)new StudentEnum(),
          'required' => false,
          'empty_value' => false
          ]);





          share|improve this answer


















          • 6




            this solution is good until Symfony 2.8, then you must use "placeholder" option, since "empty_value" has been deprecated in 2.7 (and removed in 3.0)
            – Massimiliano Arione
            Oct 13 '15 at 13:05


















          up vote
          4
          down vote













          As described here,



          required
          type: Boolean default: true


          required option default value is set to true, so you should then set it to false.



          builder->add('student', 'choice', array(
          'label'=> false,
          'expanded' => true,
          'required' => false,
          //...
          ))
          ;


          Also, you can read from documentation that,




          This is superficial and independent from validation. At best, if you let Symfony
          guess your field type, then the value of this option will be guessed from your
          validation information.




          You need then to set a validation rule that take into account the fact that your field should not be required in order to let your form set the right value of required.



          This may probably help.






          share|improve this answer






















          • I had tried this before and this adding extra radio on top and I don't want that.
            – user2156980
            Nov 4 '13 at 11:00










          • Ok, so then check the update and let me know if it helps.
            – Ahmed Siouani
            Nov 4 '13 at 11:04










          • I have set * @AssertChoice(choices = 4,3,2,1, null) but nothing changed.
            – user2156980
            Nov 4 '13 at 11:29











          • OK I have found the solution 'required' => false, 'empty_value' => false
            – user2156980
            Nov 4 '13 at 11:40










          • Thank You for your help
            – user2156980
            Nov 4 '13 at 11:40

















          up vote
          0
          down vote













          Since Symfony 3.0 empty_value is removed and you need to use placeholder instead:



           $builder
          ->add(
          'student',
          'choice',
          [
          'label'=> false,
          'expanded' => true,
          'choices' => (Array)new StudentEnum(),
          'required' => false,
          'placeholder' => null
          ]
          );





          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19766060%2fsymfony-2-form-field-radio-required-false%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            17
            down vote



            accepted










            The solution is required => false and empty_value => false



            $builder
            ->add('student', 'choice', [
            'label'=> false,
            'expanded' => true,
            'choices' => (Array)new StudentEnum(),
            'required' => false,
            'empty_value' => false
            ]);





            share|improve this answer


















            • 6




              this solution is good until Symfony 2.8, then you must use "placeholder" option, since "empty_value" has been deprecated in 2.7 (and removed in 3.0)
              – Massimiliano Arione
              Oct 13 '15 at 13:05















            up vote
            17
            down vote



            accepted










            The solution is required => false and empty_value => false



            $builder
            ->add('student', 'choice', [
            'label'=> false,
            'expanded' => true,
            'choices' => (Array)new StudentEnum(),
            'required' => false,
            'empty_value' => false
            ]);





            share|improve this answer


















            • 6




              this solution is good until Symfony 2.8, then you must use "placeholder" option, since "empty_value" has been deprecated in 2.7 (and removed in 3.0)
              – Massimiliano Arione
              Oct 13 '15 at 13:05













            up vote
            17
            down vote



            accepted







            up vote
            17
            down vote



            accepted






            The solution is required => false and empty_value => false



            $builder
            ->add('student', 'choice', [
            'label'=> false,
            'expanded' => true,
            'choices' => (Array)new StudentEnum(),
            'required' => false,
            'empty_value' => false
            ]);





            share|improve this answer














            The solution is required => false and empty_value => false



            $builder
            ->add('student', 'choice', [
            'label'=> false,
            'expanded' => true,
            'choices' => (Array)new StudentEnum(),
            'required' => false,
            'empty_value' => false
            ]);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 20:04









            Thomas Landauer

            2,26811646




            2,26811646










            answered Nov 4 '13 at 11:42









            user2156980

            2901314




            2901314







            • 6




              this solution is good until Symfony 2.8, then you must use "placeholder" option, since "empty_value" has been deprecated in 2.7 (and removed in 3.0)
              – Massimiliano Arione
              Oct 13 '15 at 13:05













            • 6




              this solution is good until Symfony 2.8, then you must use "placeholder" option, since "empty_value" has been deprecated in 2.7 (and removed in 3.0)
              – Massimiliano Arione
              Oct 13 '15 at 13:05








            6




            6




            this solution is good until Symfony 2.8, then you must use "placeholder" option, since "empty_value" has been deprecated in 2.7 (and removed in 3.0)
            – Massimiliano Arione
            Oct 13 '15 at 13:05





            this solution is good until Symfony 2.8, then you must use "placeholder" option, since "empty_value" has been deprecated in 2.7 (and removed in 3.0)
            – Massimiliano Arione
            Oct 13 '15 at 13:05













            up vote
            4
            down vote













            As described here,



            required
            type: Boolean default: true


            required option default value is set to true, so you should then set it to false.



            builder->add('student', 'choice', array(
            'label'=> false,
            'expanded' => true,
            'required' => false,
            //...
            ))
            ;


            Also, you can read from documentation that,




            This is superficial and independent from validation. At best, if you let Symfony
            guess your field type, then the value of this option will be guessed from your
            validation information.




            You need then to set a validation rule that take into account the fact that your field should not be required in order to let your form set the right value of required.



            This may probably help.






            share|improve this answer






















            • I had tried this before and this adding extra radio on top and I don't want that.
              – user2156980
              Nov 4 '13 at 11:00










            • Ok, so then check the update and let me know if it helps.
              – Ahmed Siouani
              Nov 4 '13 at 11:04










            • I have set * @AssertChoice(choices = 4,3,2,1, null) but nothing changed.
              – user2156980
              Nov 4 '13 at 11:29











            • OK I have found the solution 'required' => false, 'empty_value' => false
              – user2156980
              Nov 4 '13 at 11:40










            • Thank You for your help
              – user2156980
              Nov 4 '13 at 11:40














            up vote
            4
            down vote













            As described here,



            required
            type: Boolean default: true


            required option default value is set to true, so you should then set it to false.



            builder->add('student', 'choice', array(
            'label'=> false,
            'expanded' => true,
            'required' => false,
            //...
            ))
            ;


            Also, you can read from documentation that,




            This is superficial and independent from validation. At best, if you let Symfony
            guess your field type, then the value of this option will be guessed from your
            validation information.




            You need then to set a validation rule that take into account the fact that your field should not be required in order to let your form set the right value of required.



            This may probably help.






            share|improve this answer






















            • I had tried this before and this adding extra radio on top and I don't want that.
              – user2156980
              Nov 4 '13 at 11:00










            • Ok, so then check the update and let me know if it helps.
              – Ahmed Siouani
              Nov 4 '13 at 11:04










            • I have set * @AssertChoice(choices = 4,3,2,1, null) but nothing changed.
              – user2156980
              Nov 4 '13 at 11:29











            • OK I have found the solution 'required' => false, 'empty_value' => false
              – user2156980
              Nov 4 '13 at 11:40










            • Thank You for your help
              – user2156980
              Nov 4 '13 at 11:40












            up vote
            4
            down vote










            up vote
            4
            down vote









            As described here,



            required
            type: Boolean default: true


            required option default value is set to true, so you should then set it to false.



            builder->add('student', 'choice', array(
            'label'=> false,
            'expanded' => true,
            'required' => false,
            //...
            ))
            ;


            Also, you can read from documentation that,




            This is superficial and independent from validation. At best, if you let Symfony
            guess your field type, then the value of this option will be guessed from your
            validation information.




            You need then to set a validation rule that take into account the fact that your field should not be required in order to let your form set the right value of required.



            This may probably help.






            share|improve this answer














            As described here,



            required
            type: Boolean default: true


            required option default value is set to true, so you should then set it to false.



            builder->add('student', 'choice', array(
            'label'=> false,
            'expanded' => true,
            'required' => false,
            //...
            ))
            ;


            Also, you can read from documentation that,




            This is superficial and independent from validation. At best, if you let Symfony
            guess your field type, then the value of this option will be guessed from your
            validation information.




            You need then to set a validation rule that take into account the fact that your field should not be required in order to let your form set the right value of required.



            This may probably help.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 11:54









            Community

            11




            11










            answered Nov 4 '13 at 10:55









            Ahmed Siouani

            11.7k105166




            11.7k105166











            • I had tried this before and this adding extra radio on top and I don't want that.
              – user2156980
              Nov 4 '13 at 11:00










            • Ok, so then check the update and let me know if it helps.
              – Ahmed Siouani
              Nov 4 '13 at 11:04










            • I have set * @AssertChoice(choices = 4,3,2,1, null) but nothing changed.
              – user2156980
              Nov 4 '13 at 11:29











            • OK I have found the solution 'required' => false, 'empty_value' => false
              – user2156980
              Nov 4 '13 at 11:40










            • Thank You for your help
              – user2156980
              Nov 4 '13 at 11:40
















            • I had tried this before and this adding extra radio on top and I don't want that.
              – user2156980
              Nov 4 '13 at 11:00










            • Ok, so then check the update and let me know if it helps.
              – Ahmed Siouani
              Nov 4 '13 at 11:04










            • I have set * @AssertChoice(choices = 4,3,2,1, null) but nothing changed.
              – user2156980
              Nov 4 '13 at 11:29











            • OK I have found the solution 'required' => false, 'empty_value' => false
              – user2156980
              Nov 4 '13 at 11:40










            • Thank You for your help
              – user2156980
              Nov 4 '13 at 11:40















            I had tried this before and this adding extra radio on top and I don't want that.
            – user2156980
            Nov 4 '13 at 11:00




            I had tried this before and this adding extra radio on top and I don't want that.
            – user2156980
            Nov 4 '13 at 11:00












            Ok, so then check the update and let me know if it helps.
            – Ahmed Siouani
            Nov 4 '13 at 11:04




            Ok, so then check the update and let me know if it helps.
            – Ahmed Siouani
            Nov 4 '13 at 11:04












            I have set * @AssertChoice(choices = 4,3,2,1, null) but nothing changed.
            – user2156980
            Nov 4 '13 at 11:29





            I have set * @AssertChoice(choices = 4,3,2,1, null) but nothing changed.
            – user2156980
            Nov 4 '13 at 11:29













            OK I have found the solution 'required' => false, 'empty_value' => false
            – user2156980
            Nov 4 '13 at 11:40




            OK I have found the solution 'required' => false, 'empty_value' => false
            – user2156980
            Nov 4 '13 at 11:40












            Thank You for your help
            – user2156980
            Nov 4 '13 at 11:40




            Thank You for your help
            – user2156980
            Nov 4 '13 at 11:40










            up vote
            0
            down vote













            Since Symfony 3.0 empty_value is removed and you need to use placeholder instead:



             $builder
            ->add(
            'student',
            'choice',
            [
            'label'=> false,
            'expanded' => true,
            'choices' => (Array)new StudentEnum(),
            'required' => false,
            'placeholder' => null
            ]
            );





            share|improve this answer


























              up vote
              0
              down vote













              Since Symfony 3.0 empty_value is removed and you need to use placeholder instead:



               $builder
              ->add(
              'student',
              'choice',
              [
              'label'=> false,
              'expanded' => true,
              'choices' => (Array)new StudentEnum(),
              'required' => false,
              'placeholder' => null
              ]
              );





              share|improve this answer
























                up vote
                0
                down vote










                up vote
                0
                down vote









                Since Symfony 3.0 empty_value is removed and you need to use placeholder instead:



                 $builder
                ->add(
                'student',
                'choice',
                [
                'label'=> false,
                'expanded' => true,
                'choices' => (Array)new StudentEnum(),
                'required' => false,
                'placeholder' => null
                ]
                );





                share|improve this answer














                Since Symfony 3.0 empty_value is removed and you need to use placeholder instead:



                 $builder
                ->add(
                'student',
                'choice',
                [
                'label'=> false,
                'expanded' => true,
                'choices' => (Array)new StudentEnum(),
                'required' => false,
                'placeholder' => null
                ]
                );






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 11 at 20:09


























                community wiki





                2 revs, 2 users 96%
                k0pernikus




























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19766060%2fsymfony-2-form-field-radio-required-false%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    What does pagestruct do in Eviews?

                    Dutch intervention in Lombok and Karangasem

                    Channel Islands