I have created a custom admin form using a UI component and added a multi-select shipping method field. I’ve successfully retrieved the selected values in an array and stored them as a string in my custom table. However, when I retrieve the values in edit mode, they are displayed in an array format with double quotes. This makes it difficult to fetch the shipping methods with their codes while editing existing records. I need to store the values without double quotes in the database table. Can you please review my code and provide suggestions on how I can achieve this?
app/code/MyModule/CustomForm/etc/db_schema.xml
<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="custom_form" resource="default" engine="innodb" comment="Custom Table">
....
<column xsi:type="varchar" name="shipping_methods" nullable="true" length="255" comment="Shipping Methods"/>
....
</table>
</schema>
app/code/MyModule/CustomForm/view/adminhtml/ui_component/custom_form.xml
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
....
<fieldset name="general">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">General Information</item>
</item>
</argument>
....
<!-- Other form fields goes here -->
....
<!-- Shipping Method form fields code START -->
<field name="shipping_methods">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Shipping Carriers and Methods</item>
<item name="formElement" xsi:type="string">multiselect</item>
<item name="source" xsi:type="string">shipping_methods</item>
<item name="dataScope" xsi:type="string">shipping_methods</item>
</item>
<item name="options" xsi:type="object">MagentoShippingModelConfigSourceAllmethods</item>
</argument>
</field>
<!-- Shipping Method form fields code END -->
</fieldset>
</form>
app/code/MyModule/CustomForm/Controller/Adminhtml/Edit/Save.php
<?php
namespace MyModuleCustomFormControllerAdminhtmlEdit;
use MagentoBackendAppAction;
use MagentoBackendAppActionContext;
use MagentoFrameworkViewResultPageFactory;
....
class Save extends Action
{
protected $_resultPageFactory;
public function __construct(Context $context, PageFactory $resultPageFactory)
{
parent::__construct($context);
$this->_resultPageFactory = $resultPageFactory;
}
public function execute()
{
if (!$this->getRequest()->getPostValue()) {
$this->_redirect('mymodule_customform/*/');
}
....
$data = $this->getRequest()->getPostValue();
$id = (int)$this->getRequest()->getParam('id');
if ($id) {
/** @var $model MyModuleCustomFormModelForm */
$model = $this->formRepository->getById($id);
} else {
/** @var $model MyModuleCustomFormModelForm */
$model = $this->formRepository->getEmptyEntity();
}
$data = $this->prepareData($data);
$model->loadPost($data);
$this->_session->setPageData($model->getData());
$this->formRepository->save($model);
$this->messageManager->addSuccessMessage(__('Form Record saved.'));
$this->_session->setPageData(false);
return $resultPage;
}
/**
* Prepares specific data
*
* @param array $data
* @return array
*/
protected function prepareData($data)
{
....
if (isset($data['shipping_methods']) && !empty($data['shipping_methods']))
{
$data['shipping_methods'] = implode(',', $data['shipping_methods']);
} else {
$data['shipping_methods'] = null;
}
....
return $data;
}
}
app/code/MyModule/CustomForm/Model/Form.php
<?php
namespace MyModuleCustomFormModel;
use MagentoFrameworkDataObject;
use MagentoQuoteModelQuoteAddress;
use MagentoQuoteModelQuoteAddressRate;
use MagentoQuoteModelQuoteAddressRateResultMethod;
use MyModuleCustomFormApiDataFormInterface;
use MyModuleCustomFormApiFormEntityInterface;
class Form extends MagentoBackendModelAbstractModel implements FormInterface, FormEntityInterface
{
....
public function __construct(
Context $context,
....
)
{
parent::__construct($context);
....
}
/**
* Retrive ID
*
* @return int
*/
public function getId()
{
return $this->getData(self::ID);
}
/**
* Set ID
*
* @param int $id id
*
* @return int
*/
public function setId()
{
return $this->setData(self::ID, $id);
}
....
/**
* Retrieve Shipping Methods
*
* @return string
*/
public function getShippingMethods()
{
return $this->getData(self::SHIPPING_METHODS);
}
/**
* Set Shipping Methods
*
* @param string $shippingMethods
*
* @return string
*/
public function setShippingMethods($shippingMethods)
{
return $this->setData(self::SHIPPING_METHODS, $shippingMethods);
}
}
This stores the Shipping Methods as below in db table rows:
id | name | shipping_methods |
---|---|---|
1 | Test | “flatrate_flatrate,tablerate_bestway,freeshipping_freeshipping” |
Till here working fine, but when I fetch the Shipping Methods and convert it to array in Edit form. Then it gives me array values as below (with double quotes):
shipping_methods => Array
(
[0] => "flatrate_flatrate
[1] => tablerate_bestway
[2] => freeshipping_freeshipping"
)
Due to the double quotes I’m unable to fetch the shipping methods with codes when editing existing record.
I want the values to be stored without double quotes in the db table.
Can you guys please review and suggest me how can I fix this?