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();
}
}
You've asked a bit of a mouthful there -- probably too much for a single Stack Exchange question, so I'm going to concentrate on teaching you how to reset the initial state of the form so things no longer appear invalid if there's a blank value. If you're interested in the whys, you'll need to get up to speed on the fundamentals of UI Components and x-magento-init
scripts. My Magento 2 UI Components and uiElement Internals series are a good place to start. The following assumes you have some familiarity with these systems -- if there's something confusing below then the above articles (as well as the other Magento 2 articles) will probably have the information you're looking for.
In the Checkout Application, each form field is represented by a view model, and one-or-many view model templates. You can see this in Commerce Bug's KO Scopes tab.
The firstname's view model is an object derived from the constructor function returned by the Magento_Ui/js/form/element/abstract
RequireJS module. You can find that model's source code here
vendor/magento//module-ui/view/base/web/js/form/element/abstract.js
The reset
method is the one we're interested in. This will reset a field's validation state. You'll need to figure out the full uiRegistry
string identifier for the specific model (again, Commerce Bug is useful here), use that string to fetch the instantiated view model, and then call the reset function. For the checkmo
form, the following program (easily adapted into a define
module if you're going to use it in your application) will do that for the firstname
field.
requirejs(['uiRegistry'], function(uiRegistry){
uiRegistry.get('checkout.steps.billing-step.payment.payments-list.checkmo-form.form-fields.firstname').reset();
});
Of course, you'll likely want to fetch each view model that's part of a particular form -- the following is a start on that.
uiRegistry.get(function(viewModel){
var parentName = viewModel['parentName'] ? viewModel['parentName'] : '';
if(parentName.indexOf('checkout.steps.billing-step.payment.payments-list.checkmo-form.form-fields') === -1){
return;
};
console.log(viewModel);
});
This uses the uiRegistry
's callback feature to search for all view models with a parent of checkout.steps.billing-step.payment.payments-list.checkmo-form.form-fields
. Some of these may not be form elements, but we'll leave that problem as an exercise for the questioner.
Hope that helps!
Best Answer
You need to use change() function after val() called. When you used val() of jquery in knockout context at that time you need to use below syntax,
$('input[name="image_data"]').val(Data).change();