0

Magento 2 i have created admin grid and form. when i am trying to add new data save button not working add new->new form ->save and continue , save both not wokring

below is my code

  <?php
namespace MagePsycho\Customshipping\Block\Adminhtml\Zipcodes;

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 = 'zip_id';
        $this->_blockGroup = 'MagePsycho_Customshipping';
        $this->_controller = 'adminhtml_zipcodes';
 
        parent::_construct();
 
        if ($this->_isAllowedAction('MagePsycho_Customshipping::zipcodes_save')) {
            $this->buttonList->update('save', 'label', __('Save'));
            $this->buttonList->add(
                'save_and_continue',
                [
                    '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('deliveryzipcodes_data')->getZipId()) {
            return __("Edit Department '%1'", $this->escapeHtml($this->_coreRegistry->registry('deliveryzipcodes_data')->getZipId()));
        } 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('zips/*/save', ['_current' => true, 'back' => 'edit', 'active_tab' => '']);
    }
 
    
}
?>

below is form file

    <?php
namespace MagePsycho\Customshipping\Block\Adminhtml\Zipcodes\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('deliveryzipcodes_form');
        $this->setTitle(__('Delivery Zipcode Information'));
    }
    
    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()
    {
        /** @var \Maxime\Jobs\Model\Department $model */
        $model = $this->_coreRegistry->registry('deliveryzipcodes_data');
 
        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create(
            ['data' => ['zip_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(
            'zip_code',
            'text',
            ['name' => 'zip_code', 'label' => __('Zip Code'), 'title' => __('Zip Code')]
        );
 
        $fieldset->addField(
            'weekday_price',
            'text',
            ['name' => 'weekday_price', 'label' => __('Weekday Price'), 'title' => __('Weekday Price')]
        );
        $fieldset->addField(
            'weekend_price',
            'text',
            ['name' => 'weekend_price', 'label' => __('Weekend Price'), 'title' => __('Weekend Price')]
        );
        $fieldset->addField(
            'workwave_zips',
            'checkbox',
            ['name' => 'workwave_zips', 'label' => __('Workwave'), 'title' => __('Workwave'),'onchange' => 'this.value = this.checked;']
        );
        $fieldset->addField(
            'thirdparty_zips',
            'checkbox',
            ['name' => 'thirdparty_zips', 'label' => __('Thirdparty'), 'title' => __('Thirdparty'),'onchange' => 'this.value = this.checked;']
        );
 
        
        $form->setValues($model->getData());
        $form->setUseContainer(true);
        $this->setForm($form);
 
        return parent::_prepareForm();
    }
}
3
1

Please check this post for Admin Grid CRUD In Magento 2 : https://www.magetop.com/blog/admin-grid-crud-in-magento/

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.