Như bạn biết chúng ta có thể sử dụng ui component để tạo form, còn một cách khác nữa đó là sử block. Cách này cũng khá là đơn giản thôi. Hãy cùng tìm hiểu nhé.
Xem thêm: Tạo form admin sử dụng Ui Component
Các bước như tạo controller, routes mình sẽ không viết trong bài này các bạn có thể xem lại các bài viết trước nhé
1. Tạo layout
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block class="ViMagento\HelloWorld\Block\Adminhtml\Edit" name="form.edit"/> </referenceContainer> </body> </page> |
Tiếp theo tạo file ViMagento\HelloWorld\Block\Adminhtml\Edit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?php namespace ViMagento\HelloWorld\Block\Adminhtml; /** * Class Edit * @package ViMagento\HelloWorld\Block\Adminhtml */ class Edit extends \Magento\Backend\Block\Widget\Form\Container { /** * Internal constructor * * @return void */ protected function _construct() { parent::_construct(); /** * Xóa các button không cần thiết */ $this->buttonList->remove('back'); $this->buttonList->remove('reset'); $this->buttonList->remove('delete'); $this->_objectId = 'example'; $this->_blockGroup = 'ViMagento_HelloWorld'; /** * Đường dẫn đến file hiện tại, tính từ thư mục bên trong thư mục block, * Nếu có nhiều thư mục có thể phân cách nhau bởi dấu gạch dưới * Ví dụ: adminhtml_post_tab */ $this->_controller = 'adminhtml'; } /** * Get header text * * @return \Magento\Framework\Phrase */ public function getHeaderText() { return __('New Post'); } /** * Save path url * @return string */ public function getSaveUrl() { return $this->getUrl('*/*/save'); } } |
Ở file trên cần chú ý dòng code sau:
1 |
$this->_controller = 'adminhtml'; |
Đây là đường dẫn đến file trên của bạn. Ví dụ đường dẫn của bạn là ViMagento\HelloWorld\Block\Adminhtml\Post\Edit thì bạn phải đặt $this->_controller = 'adminhtml_post';
. Magento sẽ dựa vào biến này để tìm đến form được tạo ở bước 2.
2. Tạo form
Tạo một folder có tên trùng với file đã tạo ở bước 1. Ở trên mình có file ViMagento\HelloWorld\Block\Adminhtml\Edit.php
nên mình sẽ đặt một folder có tên là Edit và tạo một file là Form.php bên trong folder này.
ViMagento\HelloWorld\Block\Adminhtml\Edit\Form.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?php namespace ViMagento\HelloWorld\Block\Adminhtml\Edit; use Magento\Backend\Block\Widget\Form\Generic; /** * Class Form * @package ViMagento\HelloWorld\Block\Adminhtml\Edit */ class Form extends Generic { /** * @return Form * @throws \Magento\Framework\Exception\LocalizedException */ protected function _prepareForm() { $form = $this->_formFactory->create( [ 'data' => [ 'id' => 'edit_form', 'action' => $this->getUrl('*/*/save'), 'method' => 'post', 'enctype' => 'multipart/form-data' ] ] ); $form->setUseContainer(true); $fieldsets = $form->addFieldset( 'field_set_example', ['legend' => __('General')] ); $fieldsets->addField( 'name', 'text', [ 'name' => 'name', 'label' => __('Name'), 'title' => __('Name'), 'required' => true, 'value' => $this->getData('name') ] ); $fieldsets->addField( 'content', 'textarea', [ 'name' => 'content', 'title' => __('Content'), 'label' => __('Content'), 'required' => true ] ); $fieldsets->addField( 'status', 'select', [ 'name' => 'status', 'title' => __('Status'), 'label' => __('Status'), 'required' => true, 'values' => [['value' => '1', 'label' => __('Pending')], ['value' => 2, 'label' => __('Publish')]] ] ); $this->setForm($form); return parent::_prepareForm(); } } |
Làm đến đây thì chúng ta đã có được một form rồi.

3. Hiển thị dữ liệu khi edit
Đây là một phần rất quan trọng, khi edit thì phải hiển thị dữ liệu ra các field của form đúng không nào. Để làm được điều này các bạn cần thêm đoạn code này vào file Form.php
ở bước 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
.... $fieldsets->addField( 'status', 'select', [ 'name' => 'status', 'title' => __('Status'), 'label' => __('Status'), 'required' => true, 'values' => [['value' => '1', 'label' => __('Pending')], ['value' => 2, 'label' => __('Publish')]] ] ); $id = $this->getRequest()->getParam('id', false); $post = $this->postModel->create()->load($id); // Set dữ liệu cho form $form->setValues($post->getData); $this->setForm($form); return parent::_prepareForm(); .......... |
Nếu các bạn có bất kỳ thắc mắc về bài viết hoặc làm không được, hãy để lại bình luận bên dưới để được hỗ trợ nhé.