Hello,
I want to add an add/edit form to my admin panel
app/code/Maxime/Jobs/view/adminhtml/layout/jobs_department_edit.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="editor"/>
<body>
<referenceContainer name="content">
<block class="Maxime\Jobs\Block\Adminhtml\Department\Edit" name="jobs_department_edit"/>
</referenceContainer>
</body>
</page>app/code/Maxime/Jobs/Block/Adminhtml/Department/Edit.php
<?php
namespace Maxime\Jobs\Block\Adminhtml\Department;
use Magento\Backend\Block\Widget\Form\Container;
class Edit extends Container
{
/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry = null;
/**
* @param \Magento\Backend\Block\Widget\Context $context
* @param \Magento\Framework\Registry $registry
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Framework\Registry $registry,
array $data = []
) {
$this->_coreRegistry = $registry;
parent::__construct($context, $data);
}
/**
* Department edit block
*
* @return void
*/
protected function _construct()
{
$this->_objectId = 'entity_id';
$this->_blockGroup = 'Maxime_Jobs';
$this->_controller = 'adminhtml_department';
parent::_construct();
if ($this->_isAllowedAction('Maxime_Jobs::department_save')) {
$this->buttonList->update('save', 'label', __('Save Department'));
$this->buttonList->add(
'saveandcontinue',
[
'label' => __('Save and Continue Edit'),
'class' => 'save',
'data_attribute' => [
'mage-init' => [
'button' => ['event' => 'saveAndContinueEdit', 'target' => '#edit_form'],
],
]
],
-100
);
} else {
$this->buttonList->remove('save');
}
}
/**
* Get header with Department name
*
* @return \Magento\Framework\Phrase
*/
public function getHeaderText()
{
if ($this->_coreRegistry->registry('jobs_department')->getId()) {
return __("Edit Department '%1'", $this->escapeHtml($this->_coreRegistry->registry('jobs_department')->getName()));
} else {
return __('New Department');
}
}
/**
* Check permission for passed action
*
* @param string $resourceId
* @return bool
*/
protected function _isAllowedAction($resourceId)
{
return $this->_authorization->isAllowed($resourceId);
}
/**
* Getter of url for "Save and Continue" button
* tab_id will be replaced by desired by JS later
*
* @return string
*/
protected function _getSaveAndContinueUrl()
{
return $this->getUrl('jobs/*/save', ['_current' => true, 'back' => 'edit', 'active_tab' => '']);
}
}app/code/Maxime/Jobs/Block/Adminhtml/Department/Edit/Form.php
<?php
namespace Maxime\Jobs\Block\Adminhtml\Department\Edit;
use \Magento\Backend\Block\Widget\Form\Generic;
class Form extends 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\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('department_form');
$this->setTitle(__('Department Information'));
}
/**
* Prepare form
*
* @return $this
*/
protected function _prepareForm()
{
/** @var \Maxime\Jobs\Model\Department $model */
$model = $this->_coreRegistry->registry('jobs_department');
/** @var \Magento\Framework\Data\Form $form */
$form = $this->_formFactory->create(
['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
);
$form->setHtmlIdPrefix('department_');
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('General Information'), 'class' => 'fieldset-wide']
);
if ($model->getId()) {
$fieldset->addField('entity_id', 'hidden', ['name' => 'entity_id']);
}
$fieldset->addField(
'name',
'text',
['name' => 'name', 'label' => __('Department Name'), 'title' => __('Department Name'), 'required' => true]
);
$fieldset->addField(
'description',
'textarea',
['name' => 'description', 'label' => __('Department Description'), 'title' => __('Department Description'), 'required' => true]
);
$form->setValues($model->getData());
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}Reference: https://www.maximehuran.fr/en/layouts-and-forms-on-magento-2-admin/
Now My form does not display And in my system.log file I have this error
I activated the developer mode I have 2 exceptions
2 exception(s): Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid block type: Maxime\Jobs\Block\Adminhtml\Department\Edit\Form Exception #1 (ReflectionException): Class Maxime\Jobs\Block\Adminhtml\Department\Edit\Form does not exist
How can I resolve this problem?
Thank you!
You have LocalizedException issue as per your error log.
So to resolved this issue , you need to remove your var/generation , var/cache directory from the root directory of application.
Now run the below commands :
php bin/magento setup:di:compile php bin/magento cache:clean php bin/magento cache:flush
Generally LocalizedException will occur when you have issue with controller and block file with factory class.
if issue solved, Click kudos/Accept as solutions.
Got a same problem on version 2.3.2.
But after command
bin/magento setup:di:compile
error changing to:
Fatal error: Uncaught Error: Class '[Vendor]\[Namespace]\Block\Adminhtml\Vendors\Edit\Form' not found in /var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:116
Stack trace: #0 /var/www/html/vendor/magento/framework/ObjectManager/Factory/Compiled.php(108): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('[Vendor]\\[Namespace]\\B...', Array) Then command
bin/magento setup:upgrade
returns to the topic starter's exceptions
----------------------------------------------------------------------
Errors and exceptions was caused by wrong folder structure of block ![]()