12
12
use Magento \Customer \Api \Data \RegionInterface ;
13
13
use Magento \Customer \Api \Data \RegionInterfaceFactory ;
14
14
use Magento \Customer \Model \Data \Address as AddressData ;
15
+ use Magento \Framework \App \ObjectManager ;
15
16
use Magento \Framework \Model \AbstractExtensibleModel ;
16
17
17
18
/**
@@ -118,6 +119,9 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
118
119
*/
119
120
protected $ dataObjectHelper ;
120
121
122
+ /** @var CompositeValidator */
123
+ private $ compositeValidator ;
124
+
121
125
/**
122
126
* @param \Magento\Framework\Model\Context $context
123
127
* @param \Magento\Framework\Registry $registry
@@ -135,6 +139,8 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
135
139
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
136
140
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
137
141
* @param array $data
142
+ * @param CompositeValidator $compositeValidator
143
+ *
138
144
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
139
145
*/
140
146
public function __construct (
@@ -153,7 +159,8 @@ public function __construct(
153
159
\Magento \Framework \Api \DataObjectHelper $ dataObjectHelper ,
154
160
\Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
155
161
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
156
- array $ data = []
162
+ array $ data = [],
163
+ CompositeValidator $ compositeValidator = null
157
164
) {
158
165
$ this ->_directoryData = $ directoryData ;
159
166
$ data = $ this ->_implodeArrayField ($ data );
@@ -165,6 +172,8 @@ public function __construct(
165
172
$ this ->addressDataFactory = $ addressDataFactory ;
166
173
$ this ->regionDataFactory = $ regionDataFactory ;
167
174
$ this ->dataObjectHelper = $ dataObjectHelper ;
175
+ $ this ->compositeValidator = $ compositeValidator ?: ObjectManager::getInstance ()
176
+ ->get (CompositeValidator::class);
168
177
parent ::__construct (
169
178
$ context ,
170
179
$ registry ,
@@ -562,84 +571,22 @@ public function getDataModel($defaultBillingAddressId = null, $defaultShippingAd
562
571
}
563
572
564
573
/**
565
- * Validate address attribute values
566
- *
567
- *
574
+ * Validate address attribute values.
568
575
*
569
- * @return bool|array
570
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
571
- * @SuppressWarnings(PHPMD.NPathComplexity)
576
+ * @return array|bool
572
577
*/
573
578
public function validate ()
574
579
{
575
580
if ($ this ->getShouldIgnoreValidation ()) {
576
581
return true ;
577
582
}
578
-
579
- $ errors = [];
580
- if (!\Zend_Validate::is ($ this ->getFirstname (), 'NotEmpty ' )) {
581
- $ errors [] = __ ('"%fieldName" is required. Enter and try again. ' , ['fieldName ' => 'firstname ' ]);
582
- }
583
-
584
- if (!\Zend_Validate::is ($ this ->getLastname (), 'NotEmpty ' )) {
585
- $ errors [] = __ ('"%fieldName" is required. Enter and try again. ' , ['fieldName ' => 'lastname ' ]);
586
- }
587
-
588
- if (!\Zend_Validate::is ($ this ->getStreetLine (1 ), 'NotEmpty ' )) {
589
- $ errors [] = __ ('"%fieldName" is required. Enter and try again. ' , ['fieldName ' => 'street ' ]);
590
- }
591
583
592
- if (!\Zend_Validate::is ($ this ->getCity (), 'NotEmpty ' )) {
593
- $ errors [] = __ ('"%fieldName" is required. Enter and try again. ' , ['fieldName ' => 'city ' ]);
594
- }
595
-
596
- if ($ this ->isTelephoneRequired ()) {
597
- if (!\Zend_Validate::is ($ this ->getTelephone (), 'NotEmpty ' )) {
598
- $ errors [] = __ ('"%fieldName" is required. Enter and try again. ' , ['fieldName ' => 'telephone ' ]);
599
- }
600
- }
601
-
602
- if ($ this ->isFaxRequired ()) {
603
- if (!\Zend_Validate::is ($ this ->getFax (), 'NotEmpty ' )) {
604
- $ errors [] = __ ('"%fieldName" is required. Enter and try again. ' , ['fieldName ' => 'fax ' ]);
605
- }
606
- }
607
-
608
- if ($ this ->isCompanyRequired ()) {
609
- if (!\Zend_Validate::is ($ this ->getCompany (), 'NotEmpty ' )) {
610
- $ errors [] = __ ('"%fieldName" is required. Enter and try again. ' , ['fieldName ' => 'company ' ]);
611
- }
612
- }
613
-
614
- $ _havingOptionalZip = $ this ->_directoryData ->getCountriesWithOptionalZip ();
615
- if (!in_array (
616
- $ this ->getCountryId (),
617
- $ _havingOptionalZip
618
- ) && !\Zend_Validate::is (
619
- $ this ->getPostcode (),
620
- 'NotEmpty '
621
- )
622
- ) {
623
- $ errors [] = __ ('"%fieldName" is required. Enter and try again. ' , ['fieldName ' => 'postcode ' ]);
624
- }
625
-
626
- if (!\Zend_Validate::is ($ this ->getCountryId (), 'NotEmpty ' )) {
627
- $ errors [] = __ ('"%fieldName" is required. Enter and try again. ' , ['fieldName ' => 'countryId ' ]);
628
- }
629
-
630
- if ($ this ->getCountryModel ()->getRegionCollection ()->getSize () && !\Zend_Validate::is (
631
- $ this ->getRegionId (),
632
- 'NotEmpty '
633
- ) && $ this ->_directoryData ->isRegionRequired (
634
- $ this ->getCountryId ()
635
- )
636
- ) {
637
- $ errors [] = __ ('"%fieldName" is required. Enter and try again. ' , ['fieldName ' => 'regionId ' ]);
638
- }
584
+ $ errors = $ this ->compositeValidator ->validate ($ this );
639
585
640
586
if (empty ($ errors )) {
641
587
return true ;
642
588
}
589
+
643
590
return $ errors ;
644
591
}
645
592
0 commit comments