I experienced a situation were I added additional fields in the backend customer form and these were not posted with the request. The fields were neither available in the observer using the $observer->getRequest()->getPostValue()
methods neither they were showing as sent along with the request when debugging the request using Chrome Developer Tools under the network tab. All data except these custom fields was sent as it should. It seemed like the javascript class responsible for submitting the form data was ignoring any custom fields.
1 Answer
By browsing the following javascript file:
magento/vendor/magento/module-ui/view/base/web/js/form/form.js
I noticed that the submit
function was retrieving additional fields using a specific html attribute.
On line 197 the selector is set as follow:
this.selector = '[data-form-part=' + this.namespace + ']';
Solution:
We have to add this specific data-form-part
attribute to the field declaration with the form name as value in order for the field to be "selected" by the form.js
UI script. Then all posted values will be available as part of the POST request along with the initial fields.
Example:
$fieldset->addField('some_input', 'text',
[
'name' => 'some_input',
'label' => __('Some Input'),
'title' => __('Some Input'),
'required' => true,
'data-form-part' => 'customer_form' //SET THIS ATTRIBUTE
]
This solution should also be applicable to other Magento forms once we changed the customer_form
value to the name of the form we are willing to add fields to.
-
1