@@ -124,8 +124,12 @@ public function save(\Magento\Customer\Api\Data\AddressInterface $address)
124
124
$ addressModel ->updateData ($ address );
125
125
}
126
126
127
- $ inputException = $ this ->_validate ($ addressModel );
128
- if ($ inputException ->wasErrorAdded ()) {
127
+ $ errors = $ addressModel ->validate ();
128
+ if ($ errors !== true ) {
129
+ $ inputException = new InputException ();
130
+ foreach ($ errors as $ error ) {
131
+ $ inputException ->addError ($ error );
132
+ }
129
133
throw $ inputException ;
130
134
}
131
135
$ addressModel ->save ();
@@ -255,70 +259,6 @@ public function deleteById($addressId)
255
259
return true ;
256
260
}
257
261
258
- /**
259
- * Validate Customer Addresses attribute values.
260
- *
261
- * @param CustomerAddressModel $customerAddressModel the model to validate
262
- * @return InputException
263
- *
264
- * @SuppressWarnings(PHPMD.NPathComplexity)
265
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
266
- */
267
- private function _validate (CustomerAddressModel $ customerAddressModel )
268
- {
269
- $ exception = new InputException ();
270
- if ($ customerAddressModel ->getShouldIgnoreValidation ()) {
271
- return $ exception ;
272
- }
273
-
274
- if (!\Zend_Validate::is ($ customerAddressModel ->getFirstname (), 'NotEmpty ' )) {
275
- $ exception ->addError (__ ('%fieldName is a required field. ' , ['fieldName ' => 'firstname ' ]));
276
- }
277
-
278
- if (!\Zend_Validate::is ($ customerAddressModel ->getLastname (), 'NotEmpty ' )) {
279
- $ exception ->addError (__ ('%fieldName is a required field. ' , ['fieldName ' => 'lastname ' ]));
280
- }
281
-
282
- if (!\Zend_Validate::is ($ customerAddressModel ->getStreetLine (1 ), 'NotEmpty ' )) {
283
- $ exception ->addError (__ ('%fieldName is a required field. ' , ['fieldName ' => 'street ' ]));
284
- }
285
-
286
- if (!\Zend_Validate::is ($ customerAddressModel ->getCity (), 'NotEmpty ' )) {
287
- $ exception ->addError (__ ('%fieldName is a required field. ' , ['fieldName ' => 'city ' ]));
288
- }
289
-
290
- if (!\Zend_Validate::is ($ customerAddressModel ->getTelephone (), 'NotEmpty ' )) {
291
- $ exception ->addError (__ ('%fieldName is a required field. ' , ['fieldName ' => 'telephone ' ]));
292
- }
293
-
294
- $ havingOptionalZip = $ this ->directoryData ->getCountriesWithOptionalZip ();
295
- if (!in_array ($ customerAddressModel ->getCountryId (), $ havingOptionalZip )
296
- && !\Zend_Validate::is ($ customerAddressModel ->getPostcode (), 'NotEmpty ' )
297
- ) {
298
- $ exception ->addError (__ ('%fieldName is a required field. ' , ['fieldName ' => 'postcode ' ]));
299
- }
300
-
301
- if (!\Zend_Validate::is ($ customerAddressModel ->getCountryId (), 'NotEmpty ' )) {
302
- $ exception ->addError (__ ('%fieldName is a required field. ' , ['fieldName ' => 'countryId ' ]));
303
- }
304
-
305
- if ($ this ->directoryData ->isRegionRequired ($ customerAddressModel ->getCountryId ())) {
306
- $ regionCollection = $ customerAddressModel ->getCountryModel ()->getRegionCollection ();
307
- if (!$ regionCollection ->count () && empty ($ customerAddressModel ->getRegion ())) {
308
- $ exception ->addError (__ ('%fieldName is a required field. ' , ['fieldName ' => 'region ' ]));
309
- } elseif (
310
- $ regionCollection ->count ()
311
- && !in_array (
312
- $ customerAddressModel ->getRegionId (),
313
- array_column ($ regionCollection ->getData (), 'region_id ' )
314
- )
315
- ) {
316
- $ exception ->addError (__ ('%fieldName is a required field. ' , ['fieldName ' => 'regionId ' ]));
317
- }
318
- }
319
- return $ exception ;
320
- }
321
-
322
262
/**
323
263
* Retrieve collection processor
324
264
*
0 commit comments