File tree Expand file tree Collapse file tree 3 files changed +21
-7
lines changed Expand file tree Collapse file tree 3 files changed +21
-7
lines changed Original file line number Diff line number Diff line change @@ -850,8 +850,11 @@ of all cases covered:
850850 unchecked.
851851 - if there's more than one checkbox with the same ` name ` attribute, they are
852852 all treated collectively as a single form control, which returns the value
853- as an ** array** containing all the values of the selected checkboxes in the
853+ as an ** array** containing all the values of the checkboxes in the
854854 collection.
855+ - a hidden input with same name before the checkbox is allowed which is a
856+ common workaround to allow browsers to send ` false ` for unchecked
857+ checkboxes.
855858- ` <input type="radio"> ` elements are all grouped by the ` name ` attribute, and
856859 such a group treated as a single form control. This form control returns the
857860 value as a ** string** corresponding to the ` value ` attribute of the selected
Original file line number Diff line number Diff line change @@ -192,13 +192,15 @@ describe('.toHaveFormValues', () => {
192192 it ( 'allows a checkbox and a hidden input which is a common workaround so forms can send "false" for a checkbox' , ( ) => {
193193 const { container} = render ( `
194194 <form>
195- <input type="hidden" name="sample- checkbox" value=0>
196- <input type="checkbox" name="sample- checkbox" value=1>
195+ <input type="hidden" name="checkbox-with-hidden-false " value=0>
196+ <input type="checkbox" name="checkbox-with-hidden-false " value=1>
197197 </form>
198198 ` )
199199 const form = container . querySelector ( 'form' )
200200 expect ( ( ) => {
201- expect ( form ) . toHaveFormValues ( { "sample-checkbox" : 1 } )
201+ expect ( form ) . toHaveFormValues ( {
202+ 'checkbox-with-hidden-false' : [ '0' , '1' ] ,
203+ } )
202204 } ) . not . toThrowError ( )
203205 } )
204206
Original file line number Diff line number Diff line change @@ -11,9 +11,18 @@ import {
1111function getMultiElementValue ( elements ) {
1212 const types = [ ...new Set ( elements . map ( element => element . type ) ) ]
1313 if ( types . length !== 1 ) {
14- throw new Error (
15- 'Multiple form elements with the same name must be of the same type' ,
16- )
14+ if (
15+ types . length === 2 &&
16+ types [ 0 ] === 'hidden' &&
17+ types [ 1 ] === 'checkbox'
18+ ) {
19+ // Allow the special case where there's a 'checkbox' input, and a matching 'hidden' input
20+ // before it, which works around browser forms so a 'false' value is submitted.
21+ } else {
22+ throw new Error (
23+ 'Multiple form elements with the same name must be of the same type' ,
24+ )
25+ }
1726 }
1827 switch ( types [ 0 ] ) {
1928 case 'radio' : {
You can’t perform that action at this time.
0 commit comments