分类
后端

magento ui component form block创建办法

该方法 无需创建 ui component 简单直接

## 新建layout
## maijindoucustomer_proof_edit.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceContainer name="content">
            <block class="Maijindou\Customer\Block\Adminhtml\Invoices\Edit" name="maijindoucustomer_proof_form_edit"/>
        </referenceContainer>
    </body>
</page>

## 新建block


Maijindou\Customer\Block\Adminhtml\Invoices\Edit

<?php


namespace Maijindou\Customer\Block\Adminhtml\Invoices;


class Edit extends \Magento\Backend\Block\Widget\Form\Container
{
    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry;

    /**
     * Constructor
     *
     * @param \Magento\Framework\Registry $coreRegistry
     * @param \Magento\Backend\Block\Widget\Context $context
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Backend\Block\Widget\Context $context,
        $data = []
    ) {
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context, $data);
    }

    /**
     * Initialize Size chart edit block
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_objectId = 'item_id';//主键ID
        $this->_blockGroup = 'Maijindou_customer';//模块名称
        $this->_controller = 'adminhtml_invoices';// invoices 是当前主控制器
        parent::_construct();
        $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
        );
    }

    /**
     * Getter of url for "Save and Continue" button
     * tab_id will be replaced by desired by JS later
     *
     * @return string
     */
    protected function _getSaveAndContinueUrl()
    {
        //保存的URL
        return $this->getUrl('*/*/save', ['_current' => true, 'back' => 'edit', 'active_tab' => '']);
    }
}
## 新建表单
<?php


namespace Maijindou\Customer\Block\Adminhtml\Invoices\Edit;



class Form extends \Magento\Backend\Block\Widget\Form\Generic
{

    protected $invoicesLog;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Maijindou\Customer\Model\InvoicesLog $invoicesLog,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        array $data = [])
    {
        $this->invoicesLog = $invoicesLog;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    protected function _prepareForm()
    {
        $invoice_log = $this->invoicesLog->load($this->getRequest()->getParam('id'));

        $form = $this->_formFactory->create(
            [
                'data' => [
                    'id' => 'edit_form',
                    'action' => $this->getData('action'),
                    'method' => 'post',
                    'enctype' => 'multipart/form-data'
                ]
            ]
        );



        $form->setHtmlIdPrefix('mjd_');

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

        if ($invoice_log->getId()) {
            $fieldset->addField(
                'item_id',
                'hidden',
                ['name' => 'item_id']
            );
        }

        $fieldset->addField(
            'amount',
            'text',
            [
                'name'  => 'amount',
                'label' => __('Amount(USD $)'),
                'title' => __('Amount(USD $)'),
                'required' => true,
            ]
        );

        $fieldset->addField(
            'type',
            'select',
            [
                'name'     => 'type',
                'label'    => __('Counter-Party'),
                'title'    => __('Counter-Party'),
                'required' => true,
                'values'   => [
                    ['value' => 1, 'label' => __('Buyer')],
                    ['value' => 2, 'label' => __('Seller')],
                    ['value' => 3, 'label' => __('Inspection Company')],
                    ['value' => 4, 'label' => __('Shipper')],
                ],
            ]
        );

        $fieldset->addField(
            'is_pay',
            'select',
            [
                'name'     => 'is_pay',
                'label'    => __('Transaction Type'),
                'title'    => __('Transaction Type'),
                'required' => true,
                'values'   => [
                    ['value' => 1, 'label' => __('Pending')],
                    ['value' => 2, 'label' => __('Complate')],
                    ['value' => 3, 'label' => __('Cancel')],
                ],
            ]
        );

        $fieldset->addField(
            'remarks',
            'textarea',
            [
                'name'  => 'remarks',
                'label' => __('Remarks'),
                'title' => __('Remarks'),
                'required' => true,
            ]
        );

        $fieldset->addField(
            'payment_id',
            'text',
            [
                'name'  => 'payment_id',
                'label' => __('Payment ID'),
                'title' => __('Payment ID'),
                'required' => true,
            ]
        );

        $fieldset->addField(
            'payment_time',
            'date',
            [
                'name'  => 'payment_time',
                'label' => __('Payment Date'),
                'title' => __('Payment Date'),
                'required' => true,
                'date_format' => 'dd/MM/yyyy',
            ]
        );

        $payment_voucher = $invoice_log->getData('payment_voucher');
        $note = '';
        if($payment_voucher){
            $note = "<a href=\"$payment_voucher\" target=\"_blank\">View File</a>";
        }

        $fieldset->addField(
            'payment_voucher',
            'file',
            [
                'name'  => 'payment_voucher',
                'label' => __('Payment Voucher'),
                'title' => __('Payment Voucher'),
                'required' => true,
                'note' => $note
            ]
        );

        $form->setValues($invoice_log->getData());
        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}