@@ -33,7 +33,7 @@ export default class controlSelect extends control {
33
33
data . name = data . name + '[]'
34
34
}
35
35
36
- if ( type === 'checkbox-group' && data . required ) {
36
+ if ( ( type === 'checkbox-group' || type === 'radio-group' ) && data . required ) {
37
37
const self = this
38
38
const defaultOnRender = this . onRender . bind ( this )
39
39
this . onRender = function ( ) {
@@ -153,7 +153,10 @@ export default class controlSelect extends control {
153
153
* setCustomValidity for checkbox-group
154
154
*/
155
155
groupRequired ( ) {
156
- const checkboxes = this . element . getElementsByTagName ( 'input' )
156
+ const allInputs = this . element . getElementsByTagName ( 'input' )
157
+ const checkboxes = this . element . querySelectorAll ( 'input:not([type=text])' )
158
+ const otherCheckbox = this . element . querySelector ( '.other-option' )
159
+ const otherValue = this . element . querySelector ( '.other-val' )
157
160
const setValidity = ( checkbox , isValid ) => {
158
161
const minReq = control . mi18n ( 'minSelectionRequired' , 1 )
159
162
if ( ! isValid ) {
@@ -162,24 +165,32 @@ export default class controlSelect extends control {
162
165
checkbox . setCustomValidity ( '' )
163
166
}
164
167
}
165
- const toggleRequired = ( checkboxes , isValid ) => {
166
- [ ] . forEach . call ( checkboxes , cb => {
168
+ const toggleRequired = ( checkboxes , otherCheckbox , otherValue , isValid ) => {
169
+ [ ] . forEach . call ( checkboxes , cb => {
167
170
if ( isValid ) {
168
171
cb . removeAttribute ( 'required' )
169
172
} else {
170
173
cb . setAttribute ( 'required' , 'required' )
171
174
}
172
175
setValidity ( cb , isValid )
173
176
} )
177
+
178
+ if ( otherCheckbox ) {
179
+ if ( otherCheckbox . checked ) {
180
+ otherValue . setAttribute ( 'required' , 'required' )
181
+ } else {
182
+ otherValue . removeAttribute ( 'required' )
183
+ }
184
+ }
174
185
}
175
186
176
187
const toggleValid = ( ) => {
177
188
const isValid = [ ] . some . call ( checkboxes , cb => cb . checked )
178
- toggleRequired ( checkboxes , isValid )
189
+ toggleRequired ( checkboxes , otherCheckbox , otherValue , isValid )
179
190
}
180
191
181
- for ( let i = checkboxes . length - 1 ; i >= 0 ; i -- ) {
182
- checkboxes [ i ] . addEventListener ( 'change' , toggleValid )
192
+ for ( let i = allInputs . length - 1 ; i >= 0 ; i -- ) {
193
+ allInputs [ i ] . addEventListener ( 'change' , toggleValid )
183
194
}
184
195
toggleValid ( )
185
196
}
0 commit comments