0

I want to add two textboxes on single row, how can i achieve this using block classes in magento 2. I want this on admin side.

enter image description here

Here is the code

          $fieldset->addField(
            'option_1',
            'text',
            [
                'name' => 'option_1',
                'label' => __('Option 1'),
                'class' => 'txt-type',
                'title' => __('Option'),
                'required' => true,
                'maxlength' => '50',
                'container_id' => 'some_row'
            ]
        );


        $fieldset->addField(
            'option_2',
            'text',
            [
                'name' => 'option_2',
                'label' => __('Option 2'),
                'class' => 'txt-type',
                'title' => __('Option'),
                'required' => true,
                'note' => '',
                'container_id' => 'some_row'
            ]
        );
  • Are you using UI component? – Prince Patel Mar 7 '18 at 9:58
  • No block classes – Avesh Naik Mar 7 '18 at 10:03
  • @PrincePatel do you have any solution for UI component ? – SagarPPanchal Mar 8 '18 at 6:20
1

If you want to add two text boxes on the same line you have to wrap your fields in a group container, if you are planning to use UI component just add this code snippet :

<container name="option_group">
        <argument name="data" xsi:type="array">
            <item name="type" xsi:type="string">group</item>
            <item name="config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/form/components/group</item>
                <item name="required" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">220</item>
                <item name="breakLine" xsi:type="boolean">false</item>
            </item>
        </argument>

        <field name="option_1">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="additionalClasses" xsi:type="string">txt-type</item>
                    <item name="sortOrder" xsi:type="number">240</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="dataScope" xsi:type="string">option_1</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="string">true</item>
                    </item>
                </item>
            </argument>
        </field>

        <field name="option_2">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">option_2</item>
                    <item name="additionalClasses" xsi:type="string">txt-type</item>
                    <item name="sortOrder" xsi:type="number">240</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="dataScope" xsi:type="string">option_2</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="string">true</item>
                    </item>
                </item>
            </argument>
        </field>
    </container>

To achieve the same result with $fieldset->addField i think this should be doable with some tweaking by using the renderer.

Here is a link that may help you : https://belvg.com/blog/renderer-in-magento-2-0.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.