4

I am trying to set the value of Field in form using below code:

         $fieldset->addField(
        'hideGoogle',
        'text',
        [
            'name' => 'hideGoogle',
            'label' => __('Hide Google'),
            'values' => '1', 
        ]
    );

But its' not setting the value. I am also tried 'value' instead of 'values' but not working. Can anyone please tell me how can I set the field value in Magento 2 admin form?

3 Answers 3

6

You have to add below code in form file

app\code\Vendor\ModuleName\Block\Adminhtml\ModuleName\Edit\Form.php

    $model->setData('test', 'female');

Example of form file:

<?php
namespace Mystore\Slider\Block\Adminhtml\Slider\Edit;

/**
 * Adminhtml blog post edit form
 */
class Form extends \Magento\Backend\Block\Widget\Form\Generic
{

    /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
     * @param \Magento\Store\Model\System\Store $systemStore
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Store\Model\System\Store $systemStore,
        array $data = []
    ) {
        $this->_systemStore = $systemStore;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Init form
     *
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setId('mystore_slider_form');
        $this->setTitle(__('Slider Information'));
    }

    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()
    {
        /** @var \Ashsmith\Blog\Model\Post $model */
        $model = $this->_coreRegistry->registry('mystore_slider');

        if ($model->getId()) {
            $model->setData('test', 'female');
        }

        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create(
            ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post', 'enctype' => 'multipart/form-data']]
        );

        $form->setHtmlIdPrefix('mystore_slider_');

        $fieldset = $form->addFieldset(
            'base_fieldset',
            ['legend' => __('General Information'), 'class' => 'fieldset-wide']
        );

        if ($model->getSliderId()) {
            $fieldset->addField('slider_id', 'hidden', ['name' => 'slider_id']);
        }

        $fieldset->addField(
            'slider_title',
            'text',
            ['name' => 'slider_title', 'label' => __('Slider Title'), 'title' => __('Slider Title'), 'required' => true]
        );

        $fieldset->addField(
            'url',
            'text',
            [
                'name' => 'url',
                'label' => __('Slider URL'),
                'title' => __('Slider URL'),
                'required' => true,
                'class' => 'validate-xml-identifier'
            ]
        );

        $fieldset->addField(
            'image',
            'image',
            [
                'name' => 'image',
                'label' => __('Slider Image'),
                'title' => __('Slider Image'),
                'required' => true
            ]
        );


        $fieldset->addField(
            'test', 
            'radios',
            [
                'label' => __('test'),
                'title' => __('test'),
                'name' => 'test',
                'required' => true, 
                'values' => array(
                        array('value'=>'male','label'=>'Male'),
                        array('value'=>'female','label'=>'Female'),
                   )
            ]
        );

        /**
         * Check is single store mode
         */
        if (!$this->_storeManager->isSingleStoreMode()) {
            $field = $fieldset->addField(
                'store_id',
                'multiselect',
                [
                    'name' => 'stores[]',
                    'label' => __('Store View'),
                    'title' => __('Store View'),
                    'required' => true,
                    'values' => $this->_systemStore->getStoreValuesForForm(false, true)
                ]
            );
            $renderer = $this->getLayout()->createBlock(
                'Magento\Backend\Block\Store\Switcher\Form\Renderer\Fieldset\Element'
            );
            $field->setRenderer($renderer);
        } else {
            $fieldset->addField(
                'store_id',
                'hidden',
                ['name' => 'stores[]', 'value' => $this->_storeManager->getStore(true)->getId()]
            );
            $model->setStoreId($this->_storeManager->getStore(true)->getId());
        }

        $fieldset->addField(
            'is_active',
            'select',
            [
                'label' => __('Status'),
                'title' => __('Status'),
                'name' => 'is_active',
                'required' => true,
                'options' => ['1' => __('Enabled'), '0' => __('Disabled')]
            ]
        );        

        $form->setValues($model->getData());
        $form->setUseContainer(true);
        $this->setForm($form);

        return parent::_prepareForm();
    }
}
1
  • in block form , I have input field type="text" and i am trying to set some random value to that but it is not setting. below is code . Please help me :$fieldset->addField( 'category_code', 'text',['name' => 'category_code1', 'value'=> $this->vendorAutoCode(), 'label' => __('Category Code'), 'title' => __('Category Code'), 'required' => true] ); public function vendorAutoCode(){ return str_pad(rand(0,9999), 5, "0", STR_PAD_LEFT); } Commented Aug 7, 2017 at 9:41
4

you can set values of form with below code. add this code after the fields are defined. Tested...

$data=array('hideGoogle'=>1);    
$form->setValues($data);

before this code

$this->setForm($form);
2
1

Use the following:

$field = $fieldset
  ->addField(
        'hideGoogle',
        'text',
        [
            'name' => 'hideGoogle',
            'label' => __('Hide Google')
        ]
    );
$field->setValue('myvalue');

or:

$fieldset
  ->addField(
        'hideGoogle',
        'text',
        [
            'name' => 'hideGoogle',
            'label' => __('Hide Google'),
            'value' => 'myvalue'
        ]
    );

However, if you're overwriting the data later in the execution of your custom code, this won't work of course.

1
  • thanks @Milan simek its working perfect for me thanks a lot Commented Oct 26, 2017 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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