Skip to content

Commit 1cd2936

Browse files
committed
MAGETWO-48286: "Create/Edit Customer" Admin forms do not support data validation without refreshing
1 parent d6f8b63 commit 1cd2936

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

app/code/Magento/Customer/Block/Adminhtml/Edit/Tab/Newsletter.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function isHidden()
133133
* Initialize the form.
134134
*
135135
* @return $this
136+
* @SuppressWarnings(PHPMD.NPathComplexity)
136137
*/
137138
public function initForm()
138139
{
@@ -148,7 +149,7 @@ public function initForm()
148149

149150
$fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Newsletter Information')]);
150151

151-
$subscriptionField = $fieldset->addField(
152+
$fieldset->addField(
152153
'subscription',
153154
'checkbox',
154155
[
@@ -160,19 +161,13 @@ public function initForm()
160161
);
161162

162163
if ($this->customerAccountManagement->isReadOnly($customerId)) {
163-
$subscriptionField->setReadonly(true, true);
164+
$form->getElement('subscription')->setReadonly(true, true);
164165
}
165166
$isSubscribed = $subscriber->isSubscribed();
166167
$form->setValues(['subscription' => $isSubscribed ? 'true' : 'false']);
167-
$subscriptionField->setIsChecked($isSubscribed);
168+
$form->getElement('subscription')->setIsChecked($isSubscribed);
168169

169-
$data = $this->_backendSession->getCustomerFormData();
170-
if (!empty($data)) {
171-
$dataCustomerId = isset($data['customer']['entity_id']) ? $data['customer']['entity_id'] : null;
172-
if (isset($data['subscription']) && $dataCustomerId == $customerId) {
173-
$subscriptionField->setIsChecked($data['subscription']);
174-
}
175-
}
170+
$this->updateFromSession($form, $customerId);
176171

177172
$changedDate = $this->getStatusChangedDate();
178173
if ($changedDate) {
@@ -191,6 +186,24 @@ public function initForm()
191186
return $this;
192187
}
193188

189+
/**
190+
* Update form elements from session data
191+
*
192+
* @param \Magento\Framework\Data\Form $form
193+
* @param int $customerId
194+
* @return void
195+
*/
196+
protected function updateFromSession(\Magento\Framework\Data\Form $form, $customerId)
197+
{
198+
$data = $this->_backendSession->getCustomerFormData();
199+
if (!empty($data)) {
200+
$dataCustomerId = isset($data['customer']['entity_id']) ? $data['customer']['entity_id'] : null;
201+
if (isset($data['subscription']) && $dataCustomerId == $customerId) {
202+
$form->getElement('subscription')->setIsChecked($data['subscription']);
203+
}
204+
}
205+
}
206+
194207
/**
195208
* Retrieve the date when the subscriber status changed.
196209
*

app/code/Magento/Customer/Model/Customer/DataProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/**
2121
* Class DataProvider
22+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2223
*/
2324
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
2425
{

app/code/Magento/Customer/Test/Unit/Block/Adminhtml/Edit/Tab/NewsletterTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © 2015 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
// @codingStandardsIgnoreFile
8+
69
namespace Magento\Customer\Test\Unit\Block\Adminhtml\Edit\Tab;
710

811
use Magento\Backend\Model\Session;
@@ -129,6 +132,14 @@ public function testInitForm()
129132

130133
$this->backendSessionMock->expects($this->once())->method('getCustomerFormData')->willReturn(null);
131134

135+
$formMock->expects($this->once())
136+
->method('getElement')
137+
->willReturnMap(
138+
[
139+
['subscription', $elementMock],
140+
]
141+
);
142+
132143
$elementMock->expects($this->once())
133144
->method('setIsChecked')
134145
->with(true);
@@ -178,6 +189,14 @@ public function testInitFormWithCustomerFormData()
178189
'subscription' => true,
179190
]);
180191

192+
$formMock->expects($this->exactly(2))
193+
->method('getElement')
194+
->willReturnMap(
195+
[
196+
['subscription', $elementMock],
197+
]
198+
);
199+
181200
$elementMock->expects($this->exactly(2))
182201
->method('setIsChecked')
183202
->willReturnMap(

0 commit comments

Comments
 (0)