How to add radio option in Magento 2?












5















I am trying to add radio option but showing single radio without any label



$fieldset->addField(
'test',
'radio',
[
'label' => __('test'),
'title' => __('test'),
'name' => 'test',
'required' => true,
'values' => array(
array('value'=>'1','label'=>'Radio1'),
array('value'=>'2','label'=>'Radio2'),
array('value'=>'3','label'=>'Radio3'),
),
'disabled' => $isDisabled
]
);









share|improve this question





























    5















    I am trying to add radio option but showing single radio without any label



    $fieldset->addField(
    'test',
    'radio',
    [
    'label' => __('test'),
    'title' => __('test'),
    'name' => 'test',
    'required' => true,
    'values' => array(
    array('value'=>'1','label'=>'Radio1'),
    array('value'=>'2','label'=>'Radio2'),
    array('value'=>'3','label'=>'Radio3'),
    ),
    'disabled' => $isDisabled
    ]
    );









    share|improve this question



























      5












      5








      5








      I am trying to add radio option but showing single radio without any label



      $fieldset->addField(
      'test',
      'radio',
      [
      'label' => __('test'),
      'title' => __('test'),
      'name' => 'test',
      'required' => true,
      'values' => array(
      array('value'=>'1','label'=>'Radio1'),
      array('value'=>'2','label'=>'Radio2'),
      array('value'=>'3','label'=>'Radio3'),
      ),
      'disabled' => $isDisabled
      ]
      );









      share|improve this question
















      I am trying to add radio option but showing single radio without any label



      $fieldset->addField(
      'test',
      'radio',
      [
      'label' => __('test'),
      'title' => __('test'),
      'name' => 'test',
      'required' => true,
      'values' => array(
      array('value'=>'1','label'=>'Radio1'),
      array('value'=>'2','label'=>'Radio2'),
      array('value'=>'3','label'=>'Radio3'),
      ),
      'disabled' => $isDisabled
      ]
      );






      magento2 magento-2.0 adminform






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 9 mins ago









      magefms

      1,254220




      1,254220










      asked May 4 '16 at 10:34









      Qaisar SattiQaisar Satti

      26.8k1256109




      26.8k1256109






















          3 Answers
          3






          active

          oldest

          votes


















          5














          change type from radio to radios



          $fieldset->addField(
          'test',
          'radios',
          [
          'label' => __('test'),
          'title' => __('test'),
          'name' => 'test',
          'required' => true,
          'values' => array(
          array('value'=>'1','label'=>'Radio1'),
          array('value'=>'2','label'=>'Radio2'),
          array('value'=>'3','label'=>'Radio3'),
          ),
          'disabled' => $isDisabled
          ]
          );


          for more types you can look in to namespace MagentoFrameworkDataFormElement






          share|improve this answer































            2














            Use below code:



            $fieldset->addField(
            'test',
            'radios',
            [
            'name' => 'test',
            'label' => __('Radio'),
            'title' => __('Radio'),
            'required' => true,
            'values' => [
            ['value' =>1, 'label' => __('Yes')],
            ['value' => 0, 'label' => __('No')]
            ],
            'disabled' => $isElementDisabled
            ]
            );


            enter image description here






            share|improve this answer
























            • could you guide me to get all radios in single row as all my radios are taking 1 row each

              – Mohammad Mujassam
              May 4 '16 at 11:09













            • Use above code which i have added in answer

              – Prashant Valanda
              May 4 '16 at 11:10











            • It will display only two option in one row if options are more than 2 it will not display in single row

              – Prashant Valanda
              May 4 '16 at 11:15











            • @PrashantValanda you code is not matching with your output image

              – Qaisar Satti
              May 4 '16 at 11:16











            • It is same as image only label is changed because while i capturing snap at that time label is other. :)

              – Prashant Valanda
              May 4 '16 at 11:16



















            2














            It seems like the way Magento 2 handles radio require multiple radio fields with the same name.



            There's a good example in app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/AttributeSet/Form.php



            In your case the right code would be:



                $fieldset->addField(
            'Radio1',
            'radio',
            [
            'name' => 'test',
            'value' => '1'
            ]
            );
            $fieldset->addField(
            'Radio2',
            'radio',
            [
            'name' => 'test',
            'value' => '2'
            ]
            );
            $fieldset->addField(
            'Radio3',
            'radio',
            [
            'name' => 'test',
            'value' => '3'
            ]
            );


            On top of that it seems like app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Weight.php uses a slightly different method in the below code but as it is a custom element I'm not sure if that will fit your needs:



                $this->weightSwitcher = $factoryElement->create('radios');
            $this->weightSwitcher->setValue(
            WeightResolver::HAS_WEIGHT
            )->setValues(
            [
            ['value' => WeightResolver::HAS_WEIGHT, 'label' => __('Yes')],
            ['value' => WeightResolver::HAS_NO_WEIGHT, 'label' => __('No')]
            ]
            )->setId(
            'weight-switcher'
            )->setName(
            'product_has_weight'
            )->setLabel(
            __('Does this have a weight?')
            );





            share|improve this answer























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "479"
              };
              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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2fmagento.stackexchange.com%2fquestions%2f113902%2fhow-to-add-radio-option-in-magento-2%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









              5














              change type from radio to radios



              $fieldset->addField(
              'test',
              'radios',
              [
              'label' => __('test'),
              'title' => __('test'),
              'name' => 'test',
              'required' => true,
              'values' => array(
              array('value'=>'1','label'=>'Radio1'),
              array('value'=>'2','label'=>'Radio2'),
              array('value'=>'3','label'=>'Radio3'),
              ),
              'disabled' => $isDisabled
              ]
              );


              for more types you can look in to namespace MagentoFrameworkDataFormElement






              share|improve this answer




























                5














                change type from radio to radios



                $fieldset->addField(
                'test',
                'radios',
                [
                'label' => __('test'),
                'title' => __('test'),
                'name' => 'test',
                'required' => true,
                'values' => array(
                array('value'=>'1','label'=>'Radio1'),
                array('value'=>'2','label'=>'Radio2'),
                array('value'=>'3','label'=>'Radio3'),
                ),
                'disabled' => $isDisabled
                ]
                );


                for more types you can look in to namespace MagentoFrameworkDataFormElement






                share|improve this answer


























                  5












                  5








                  5







                  change type from radio to radios



                  $fieldset->addField(
                  'test',
                  'radios',
                  [
                  'label' => __('test'),
                  'title' => __('test'),
                  'name' => 'test',
                  'required' => true,
                  'values' => array(
                  array('value'=>'1','label'=>'Radio1'),
                  array('value'=>'2','label'=>'Radio2'),
                  array('value'=>'3','label'=>'Radio3'),
                  ),
                  'disabled' => $isDisabled
                  ]
                  );


                  for more types you can look in to namespace MagentoFrameworkDataFormElement






                  share|improve this answer













                  change type from radio to radios



                  $fieldset->addField(
                  'test',
                  'radios',
                  [
                  'label' => __('test'),
                  'title' => __('test'),
                  'name' => 'test',
                  'required' => true,
                  'values' => array(
                  array('value'=>'1','label'=>'Radio1'),
                  array('value'=>'2','label'=>'Radio2'),
                  array('value'=>'3','label'=>'Radio3'),
                  ),
                  'disabled' => $isDisabled
                  ]
                  );


                  for more types you can look in to namespace MagentoFrameworkDataFormElement







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 4 '16 at 10:55









                  R TR T

                  93711225




                  93711225

























                      2














                      Use below code:



                      $fieldset->addField(
                      'test',
                      'radios',
                      [
                      'name' => 'test',
                      'label' => __('Radio'),
                      'title' => __('Radio'),
                      'required' => true,
                      'values' => [
                      ['value' =>1, 'label' => __('Yes')],
                      ['value' => 0, 'label' => __('No')]
                      ],
                      'disabled' => $isElementDisabled
                      ]
                      );


                      enter image description here






                      share|improve this answer
























                      • could you guide me to get all radios in single row as all my radios are taking 1 row each

                        – Mohammad Mujassam
                        May 4 '16 at 11:09













                      • Use above code which i have added in answer

                        – Prashant Valanda
                        May 4 '16 at 11:10











                      • It will display only two option in one row if options are more than 2 it will not display in single row

                        – Prashant Valanda
                        May 4 '16 at 11:15











                      • @PrashantValanda you code is not matching with your output image

                        – Qaisar Satti
                        May 4 '16 at 11:16











                      • It is same as image only label is changed because while i capturing snap at that time label is other. :)

                        – Prashant Valanda
                        May 4 '16 at 11:16
















                      2














                      Use below code:



                      $fieldset->addField(
                      'test',
                      'radios',
                      [
                      'name' => 'test',
                      'label' => __('Radio'),
                      'title' => __('Radio'),
                      'required' => true,
                      'values' => [
                      ['value' =>1, 'label' => __('Yes')],
                      ['value' => 0, 'label' => __('No')]
                      ],
                      'disabled' => $isElementDisabled
                      ]
                      );


                      enter image description here






                      share|improve this answer
























                      • could you guide me to get all radios in single row as all my radios are taking 1 row each

                        – Mohammad Mujassam
                        May 4 '16 at 11:09













                      • Use above code which i have added in answer

                        – Prashant Valanda
                        May 4 '16 at 11:10











                      • It will display only two option in one row if options are more than 2 it will not display in single row

                        – Prashant Valanda
                        May 4 '16 at 11:15











                      • @PrashantValanda you code is not matching with your output image

                        – Qaisar Satti
                        May 4 '16 at 11:16











                      • It is same as image only label is changed because while i capturing snap at that time label is other. :)

                        – Prashant Valanda
                        May 4 '16 at 11:16














                      2












                      2








                      2







                      Use below code:



                      $fieldset->addField(
                      'test',
                      'radios',
                      [
                      'name' => 'test',
                      'label' => __('Radio'),
                      'title' => __('Radio'),
                      'required' => true,
                      'values' => [
                      ['value' =>1, 'label' => __('Yes')],
                      ['value' => 0, 'label' => __('No')]
                      ],
                      'disabled' => $isElementDisabled
                      ]
                      );


                      enter image description here






                      share|improve this answer













                      Use below code:



                      $fieldset->addField(
                      'test',
                      'radios',
                      [
                      'name' => 'test',
                      'label' => __('Radio'),
                      'title' => __('Radio'),
                      'required' => true,
                      'values' => [
                      ['value' =>1, 'label' => __('Yes')],
                      ['value' => 0, 'label' => __('No')]
                      ],
                      'disabled' => $isElementDisabled
                      ]
                      );


                      enter image description here







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered May 4 '16 at 10:51









                      Prashant ValandaPrashant Valanda

                      9,80712355




                      9,80712355













                      • could you guide me to get all radios in single row as all my radios are taking 1 row each

                        – Mohammad Mujassam
                        May 4 '16 at 11:09













                      • Use above code which i have added in answer

                        – Prashant Valanda
                        May 4 '16 at 11:10











                      • It will display only two option in one row if options are more than 2 it will not display in single row

                        – Prashant Valanda
                        May 4 '16 at 11:15











                      • @PrashantValanda you code is not matching with your output image

                        – Qaisar Satti
                        May 4 '16 at 11:16











                      • It is same as image only label is changed because while i capturing snap at that time label is other. :)

                        – Prashant Valanda
                        May 4 '16 at 11:16



















                      • could you guide me to get all radios in single row as all my radios are taking 1 row each

                        – Mohammad Mujassam
                        May 4 '16 at 11:09













                      • Use above code which i have added in answer

                        – Prashant Valanda
                        May 4 '16 at 11:10











                      • It will display only two option in one row if options are more than 2 it will not display in single row

                        – Prashant Valanda
                        May 4 '16 at 11:15











                      • @PrashantValanda you code is not matching with your output image

                        – Qaisar Satti
                        May 4 '16 at 11:16











                      • It is same as image only label is changed because while i capturing snap at that time label is other. :)

                        – Prashant Valanda
                        May 4 '16 at 11:16

















                      could you guide me to get all radios in single row as all my radios are taking 1 row each

                      – Mohammad Mujassam
                      May 4 '16 at 11:09







                      could you guide me to get all radios in single row as all my radios are taking 1 row each

                      – Mohammad Mujassam
                      May 4 '16 at 11:09















                      Use above code which i have added in answer

                      – Prashant Valanda
                      May 4 '16 at 11:10





                      Use above code which i have added in answer

                      – Prashant Valanda
                      May 4 '16 at 11:10













                      It will display only two option in one row if options are more than 2 it will not display in single row

                      – Prashant Valanda
                      May 4 '16 at 11:15





                      It will display only two option in one row if options are more than 2 it will not display in single row

                      – Prashant Valanda
                      May 4 '16 at 11:15













                      @PrashantValanda you code is not matching with your output image

                      – Qaisar Satti
                      May 4 '16 at 11:16





                      @PrashantValanda you code is not matching with your output image

                      – Qaisar Satti
                      May 4 '16 at 11:16













                      It is same as image only label is changed because while i capturing snap at that time label is other. :)

                      – Prashant Valanda
                      May 4 '16 at 11:16





                      It is same as image only label is changed because while i capturing snap at that time label is other. :)

                      – Prashant Valanda
                      May 4 '16 at 11:16











                      2














                      It seems like the way Magento 2 handles radio require multiple radio fields with the same name.



                      There's a good example in app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/AttributeSet/Form.php



                      In your case the right code would be:



                          $fieldset->addField(
                      'Radio1',
                      'radio',
                      [
                      'name' => 'test',
                      'value' => '1'
                      ]
                      );
                      $fieldset->addField(
                      'Radio2',
                      'radio',
                      [
                      'name' => 'test',
                      'value' => '2'
                      ]
                      );
                      $fieldset->addField(
                      'Radio3',
                      'radio',
                      [
                      'name' => 'test',
                      'value' => '3'
                      ]
                      );


                      On top of that it seems like app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Weight.php uses a slightly different method in the below code but as it is a custom element I'm not sure if that will fit your needs:



                          $this->weightSwitcher = $factoryElement->create('radios');
                      $this->weightSwitcher->setValue(
                      WeightResolver::HAS_WEIGHT
                      )->setValues(
                      [
                      ['value' => WeightResolver::HAS_WEIGHT, 'label' => __('Yes')],
                      ['value' => WeightResolver::HAS_NO_WEIGHT, 'label' => __('No')]
                      ]
                      )->setId(
                      'weight-switcher'
                      )->setName(
                      'product_has_weight'
                      )->setLabel(
                      __('Does this have a weight?')
                      );





                      share|improve this answer




























                        2














                        It seems like the way Magento 2 handles radio require multiple radio fields with the same name.



                        There's a good example in app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/AttributeSet/Form.php



                        In your case the right code would be:



                            $fieldset->addField(
                        'Radio1',
                        'radio',
                        [
                        'name' => 'test',
                        'value' => '1'
                        ]
                        );
                        $fieldset->addField(
                        'Radio2',
                        'radio',
                        [
                        'name' => 'test',
                        'value' => '2'
                        ]
                        );
                        $fieldset->addField(
                        'Radio3',
                        'radio',
                        [
                        'name' => 'test',
                        'value' => '3'
                        ]
                        );


                        On top of that it seems like app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Weight.php uses a slightly different method in the below code but as it is a custom element I'm not sure if that will fit your needs:



                            $this->weightSwitcher = $factoryElement->create('radios');
                        $this->weightSwitcher->setValue(
                        WeightResolver::HAS_WEIGHT
                        )->setValues(
                        [
                        ['value' => WeightResolver::HAS_WEIGHT, 'label' => __('Yes')],
                        ['value' => WeightResolver::HAS_NO_WEIGHT, 'label' => __('No')]
                        ]
                        )->setId(
                        'weight-switcher'
                        )->setName(
                        'product_has_weight'
                        )->setLabel(
                        __('Does this have a weight?')
                        );





                        share|improve this answer


























                          2












                          2








                          2







                          It seems like the way Magento 2 handles radio require multiple radio fields with the same name.



                          There's a good example in app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/AttributeSet/Form.php



                          In your case the right code would be:



                              $fieldset->addField(
                          'Radio1',
                          'radio',
                          [
                          'name' => 'test',
                          'value' => '1'
                          ]
                          );
                          $fieldset->addField(
                          'Radio2',
                          'radio',
                          [
                          'name' => 'test',
                          'value' => '2'
                          ]
                          );
                          $fieldset->addField(
                          'Radio3',
                          'radio',
                          [
                          'name' => 'test',
                          'value' => '3'
                          ]
                          );


                          On top of that it seems like app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Weight.php uses a slightly different method in the below code but as it is a custom element I'm not sure if that will fit your needs:



                              $this->weightSwitcher = $factoryElement->create('radios');
                          $this->weightSwitcher->setValue(
                          WeightResolver::HAS_WEIGHT
                          )->setValues(
                          [
                          ['value' => WeightResolver::HAS_WEIGHT, 'label' => __('Yes')],
                          ['value' => WeightResolver::HAS_NO_WEIGHT, 'label' => __('No')]
                          ]
                          )->setId(
                          'weight-switcher'
                          )->setName(
                          'product_has_weight'
                          )->setLabel(
                          __('Does this have a weight?')
                          );





                          share|improve this answer













                          It seems like the way Magento 2 handles radio require multiple radio fields with the same name.



                          There's a good example in app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/AttributeSet/Form.php



                          In your case the right code would be:



                              $fieldset->addField(
                          'Radio1',
                          'radio',
                          [
                          'name' => 'test',
                          'value' => '1'
                          ]
                          );
                          $fieldset->addField(
                          'Radio2',
                          'radio',
                          [
                          'name' => 'test',
                          'value' => '2'
                          ]
                          );
                          $fieldset->addField(
                          'Radio3',
                          'radio',
                          [
                          'name' => 'test',
                          'value' => '3'
                          ]
                          );


                          On top of that it seems like app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Weight.php uses a slightly different method in the below code but as it is a custom element I'm not sure if that will fit your needs:



                              $this->weightSwitcher = $factoryElement->create('radios');
                          $this->weightSwitcher->setValue(
                          WeightResolver::HAS_WEIGHT
                          )->setValues(
                          [
                          ['value' => WeightResolver::HAS_WEIGHT, 'label' => __('Yes')],
                          ['value' => WeightResolver::HAS_NO_WEIGHT, 'label' => __('No')]
                          ]
                          )->setId(
                          'weight-switcher'
                          )->setName(
                          'product_has_weight'
                          )->setLabel(
                          __('Does this have a weight?')
                          );






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered May 4 '16 at 10:52









                          Raphael at Digital PianismRaphael at Digital Pianism

                          54.6k22119277




                          54.6k22119277






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Magento Stack Exchange!


                              • 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%2fmagento.stackexchange.com%2fquestions%2f113902%2fhow-to-add-radio-option-in-magento-2%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







                              Popular posts from this blog

                              What other Star Trek series did the main TNG cast show up in?

                              Berlina muro

                              Berlina aerponto