<?php
namespace Meetanshi\Registration\Block\Adminhtml\Registration\Edit;
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;
use Magento\Framework\Data\FormFactory;
use Magento\Cms\Model\Wysiwyg\Config;
use Meetanshi\Registration\Model\Registration;
use Magento\Directory\Model\RegionFactory;
use Magento\Directory\Model\Config\Source\Country;
class Form extends Generic
{
protected $_systemStore;
protected $regionFactory;
protected $countryFactory;
public function __construct(
Context $context,
Registry $registry,
FormFactory $formFactory,
Config $wysiwygConfig,
Registration $options,RegionFactory $regionFactory,Country $countryFactory,
array $data = []
)
{
$this->_options = $options;
$this->_wysiwygConfig = $wysiwygConfig;
$this->regionFactory = $regionFactory;
$this->countryFactory = $countryFactory;
parent::__construct($context, $registry, $formFactory, $data);
}
protected function _prepareForm()
{
//$dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
$model = $this->_coreRegistry->registry('row_data');
try {
$form = $this->_formFactory->create(
['data' => [
'id' => 'edit_form',
'enctype' => 'multipart/form-data',
'action' => $this->getData('action'),
'method' => 'post'
]
]
);
}
catch (\Exception $e){
\Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info($e->getMessage());
}
$form->setHtmlIdPrefix('wkgrid_');
if ($model->getId()) {
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('Edit Row Data'), 'class' => 'fieldset-wide']
);
$fieldset->addField('id', 'hidden', ['name' => 'id']);
} else {
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('Add Row Data'), 'class' => 'fieldset-wide']
);
}
$fieldset->addField(
'title',
'select',
[
'name' => 'title',
'label' => __('Title'),
'id' => 'title',
'title' => __('Title'),
'class' => 'required-entry',
'values' => ["mr"=>'Mr',"ms"=>'Ms',"mrs"=>'Mrs'],
'required' => true,
]
);
$fieldset->addField(
'firstname',
'text',
[
'name' => 'firstname',
'label' => __('First Name'),
'id' => 'firstname',
'title' => __('First Name'),
'class' => 'required-entry validate-alpha',
'required' => true,
]
);
$fieldset->addField(
'lastname',
'text',
[
'name' => 'lastname',
'label' => __('Last Name'),
'id' => 'lastname',
'title' => __('Last Name'),
'class' => 'required-entry',
'required' => true,
]
);
$fieldset->addField(
'email',
'text',
[
'name' => 'email',
'label' => __('Email'),
'id' => 'email',
'title' => __('Email'),
'class' => 'required-entry',
'required' => true,
]
);
$fieldset->addField('gender', 'radios', array(
'label' => __('Radios'),
'name' => 'gender',
'values' => array(
array('value'=>'male','label'=>'Male'),
array('value'=>'female','label'=>'Female'),
),
'disabled' => false,
'readonly' => false
));
$fieldset->addField(
'dob',
'date',
[
'name' => 'dob',
'label' => __('DOB'),
//'date_format' => $dateFormat,
'date_format' => 'd-MM-y',
'class' => 'validate-date validate-date-range date-range-custom_theme-from',
'class' => 'required-entry',
'style' => 'width:200px',
]
);
$fieldset->addField(
'address',
'text',
[
'name' => 'address',
'label' => __('Address'),
'id' => 'address',
'title' => __('address'),
'class' => 'required-entry',
'required' => true,
]
);
$fieldset->addField(
'address2',
'text',
[
'name' => 'address2',
'label' => __(''),
'id' => 'address2',
'title' => __('address2'),
'class' => 'required-entry',
'required' => true,
]
);
$fieldset->addField(
'city',
'text',
[
'name' => 'city',
'label' => __('City'),
'id' => 'city',
'title' => __('city'),
'class' => 'required-entry',
'required' => true,
]
);
$fieldset->addField(
'zip',
'text',
[
'name' => 'zip',
'label' => __('Zip'),
'id' => 'zip',
'title' => __('Zip'),
'class' => 'required-entry',
'required' => true,
]
);
$countries = $this->countryFactory->toOptionArray();
$countryData = $fieldset->addField(
'country',
'select',
[
'name' => 'country',
'data-form-part' => $this->getData('target_form'),
'title' => __('Country'),
'label' => __('Country'),
'value' => $form['country'],
'values' => $countries
]
);
$regionCollection = $this->regionFactory->create()->getCollection()->addCountryFilter($form['country']);
$regions = $regionCollection->toOptionArray();
$fieldset->addField(
'state',
'select',
[
'name' => 'state',
'data-form-part' => $this->getData('target_form'),
'title' => __('State'),
'label' => __('State'),
'values' => $regions,
'value' => $form['state'],
]
);
$countryData->setAfterElementHtml("
<script type=\"text/javascript\">
require([
'jquery',
'mage/template',
'jquery/ui',
'mage/translate'
],
function($, mageTemplate) {
$('#edit_form').on('change', '#country_id', function(event){
$.ajax({
url : '". $this->getUrl('*/*/regionlist') . "country/' + $('#country_id').val(),
type: 'get',
dataType: 'json',
showLoader:true,
success: function(data){
$('#region_id').empty();
$('#region_id').append(data.htmlconent);
}
});
})
}
);
</script>"
);
$fieldset->addField(
'phone_no',
'text',
[
'name' => 'phone_no',
'label' => __('Phone No'),
'id' => 'phone_no',
'title' => __('phone_no'),
'class' => 'required-entry',
'required' => true,
]
);
$form->setValues($model->getData());
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
On Change ajex not working