@@ -11,6 +11,8 @@ define([
11
11
12
12
return Abstract . extend ( {
13
13
/**
14
+ * Extended list of Listeners
15
+ *
14
16
* @return {this }
15
17
*/
16
18
initListeners : function ( ) {
@@ -21,26 +23,40 @@ define([
21
23
return this ;
22
24
} ,
23
25
26
+ /**
27
+ * Fix _postcode_ depend on _country_id_ change:
28
+ * - If country in list "Zip/Postal Code is Optional countries" then
29
+ * - field "postcode" should not be required
30
+ *
31
+ * @returns {this }
32
+ */
24
33
update : function ( ) {
25
34
var parentScope = this . getPart ( this . getPart ( this . name , - 2 ) , - 2 ) ,
26
- countryComponent = registry . get ( parentScope + '.country_id.0' ) ,
27
- value = countryComponent . value ( ) ,
28
- element ;
29
-
30
- countryComponent
31
- . options ( )
32
- . some ( function ( el ) {
33
- element = el ;
34
- return el . value === value ;
35
- } ) ;
36
-
37
- if ( ! element . is_region_required ) {
38
- this . error ( false ) ;
39
- this . validation = _ . omit ( this . validation , 'required-entry' ) ;
40
- } else {
41
- this . validation [ 'required-entry' ] = true ;
42
- }
43
- this . required ( ! ! element . is_region_required ) ;
35
+ option ,
36
+ postcode = this ;
37
+
38
+ registry . get ( parentScope + '.country_id.0' , function ( countryComponent ) {
39
+ var value = countryComponent . value ( ) ;
40
+
41
+ if ( ! value ) { // empty value discard logic
42
+ return ;
43
+ }
44
+
45
+ countryComponent
46
+ . options ( )
47
+ . some ( function ( el ) {
48
+ option = el ;
49
+ return el . value === value ;
50
+ } ) ;
51
+
52
+ if ( ! option . is_region_required ) {
53
+ postcode . error ( false ) ;
54
+ postcode . validation = _ . omit ( postcode . validation , 'required-entry' ) ;
55
+ } else {
56
+ postcode . validation [ 'required-entry' ] = true ;
57
+ }
58
+ postcode . required ( ! ! option . is_region_required ) ;
59
+ } ) ;
44
60
45
61
return this ;
46
62
}
0 commit comments