@@ -20,7 +20,9 @@ QUnit.module('Validate SimpleForm Bootstrap 4', {
2020 } ,
2121 validators : {
2222 'user[name]' : { presence : [ { message : 'must be present' } ] , format : [ { message : 'is invalid' , 'with' : { options : 'g' , source : '\\d+' } } ] } ,
23- 'user[username]' : { presence : [ { message : 'must be present' } ] }
23+ 'user[username]' : { presence : [ { message : 'must be present' } ] } ,
24+ 'user[date_of_birth]' : { presence : [ { message : 'must be present' } ] } ,
25+ 'user[time_of_birth]' : { presence : [ { message : 'must be present' } ] }
2426 }
2527 }
2628
@@ -49,8 +51,81 @@ QUnit.module('Validate SimpleForm Bootstrap 4', {
4951 . append (
5052 $ ( '<span>' , { 'class' : 'input-group-text' , text : '@' } ) ) )
5153 . append (
52- $ ( '<input />' , { 'class' : 'form-control' , name : 'user[username]' , id : 'user_username' , type : 'text' } ) ) ) ) )
53-
54+ $ ( '<input />' , { 'class' : 'form-control' , name : 'user[username]' , id : 'user_username' , type : 'text' } ) ) ) )
55+ . append ( $ ( '<div>' , {
56+ class :'form-group date required user_date_of_birth'
57+ } )
58+ . append ( $ ( '<label class="form-control-label date required" for="user_date_of_birth_1i">Date of birth <abbr title="required">*</abbr></label>' ) )
59+ . append ( $ ( '<div>' , {
60+ class : 'd-flex flex-row justify-content-between align-items-center'
61+ } )
62+ . append ( $ ( '<select>' , {
63+ id : 'user_date_of_birth_1i' ,
64+ name : 'user[date_of_birth(1i)]' ,
65+ class : 'form-control mx-1 date required' ,
66+ 'data-client-side-validations-wrapper' : 'vertical_multi_select'
67+ } )
68+ . append ( $ ( '<option value=""></option>' ) )
69+ . append ( $ ( '<option value="2015">2015</option>' ) )
70+ . append ( $ ( '<option value="2025">2025</option>' ) ) )
71+ . append ( $ ( '<select>' , {
72+ id : 'user_date_of_birth_2i' ,
73+ name : 'user[date_of_birth(2i)]' ,
74+ class : 'form-control mx-1 date required' ,
75+ 'data-client-side-validations-wrapper' : 'vertical_multi_select'
76+ } )
77+ . append ( $ ( '<option value=""></option>' ) )
78+ . append ( $ ( '<option value="1">January</option>' ) )
79+ . append ( $ ( '<option value="2">February</option>' ) ) )
80+ . append ( $ ( '<select>' , {
81+ id : 'user_date_of_birth_3i' ,
82+ name : 'user[date_of_birth(3i)]' ,
83+ class : 'form-control mx-1 date required' ,
84+ 'data-client-side-validations-wrapper' : 'vertical_multi_select'
85+ } )
86+ . append ( $ ( '<option value=""></option>' ) )
87+ . append ( $ ( '<option value="1">1</option>' ) )
88+ . append ( $ ( '<option value="2">2</option>' ) )
89+ . append ( $ ( '<option value="3">3</option>' ) ) )
90+ )
91+ . append ( '<small class="form-text text-muted">hint text</small>' )
92+ )
93+ . append ( $ ( '<div>' , {
94+ class :'form-group time required user_time_of_birth form-group-invalid'
95+ } )
96+ . append ( $ ( '<label class="form-control-label time required" for="user_time_of_birth_4i">Time of birth <abbr title="required">*</abbr></label>' ) )
97+ . append ( $ ( '<div>' , {
98+ class : 'd-flex flex-row justify-content-between align-items-center'
99+ } )
100+ . append ( '<input type="hidden" id="user_time_of_birth_1i" name="user[time_of_birth(1i)]" value="1">' )
101+ . append ( '<input type="hidden" id="user_time_of_birth_2i" name="user[time_of_birth(2i)]" value="1">' )
102+ . append ( '<input type="hidden" id="user_time_of_birth_3i" name="user[time_of_birth(3i)]" value="1">' )
103+ . append ( $ ( '<select>' , {
104+ id : 'user_time_of_birth_4i' ,
105+ name : 'user[time_of_birth(4i)]' ,
106+ class : 'form-control mx-1 is-invalid time required' ,
107+ 'data-client-side-validations-wrapper' : 'vertical_multi_select'
108+ } )
109+ . append ( $ ( '<option value=""></option>' ) )
110+ . append ( $ ( '<option value="00">00</option>' ) )
111+ . append ( $ ( '<option value="23">23</option>' ) )
112+ )
113+ . append ( ':' )
114+ . append ( $ ( '<select>' , {
115+ id : 'user_time_of_birth_5i' ,
116+ name : 'user[time_of_birth(5i)]' ,
117+ class : 'form-control mx-1 is-invalid time required' ,
118+ 'data-client-side-validations-wrapper' : 'vertical_multi_select'
119+ } )
120+ . append ( $ ( '<option value=""></option>' ) )
121+ . append ( $ ( '<option value="00">00</option>' ) )
122+ . append ( $ ( '<option value="55">55</option>' ) )
123+ )
124+ )
125+ . append ( '<div class="invalid-feedback d-block">Time of birth must be present.</div>' )
126+ . append ( '<small class="form-text text-muted">Hint: At what time you were born?</small>' )
127+ )
128+ )
54129 $ ( 'form#new_user' ) . validate ( )
55130 }
56131} )
@@ -99,7 +174,7 @@ for (var i = 0; i < wrappers.length; i++) {
99174 assert . ok ( input . parent ( ) . hasClass ( 'form-group-invalid' ) )
100175 assert . ok ( label . parent ( ) . hasClass ( 'form-group-invalid' ) )
101176 assert . ok ( input . parent ( ) . find ( 'div.invalid-feedback:contains("is invalid")' ) . length === 1 )
102- assert . ok ( form . find ( 'div.invalid-feedback' ) . length === 1 )
177+ assert . ok ( input . closest ( '. form-group' ) . find ( 'div.invalid-feedback' ) . length === 1 )
103178 } )
104179
105180 QUnit . test ( wrapper + ' - Validate input-group' , function ( assert ) {
@@ -116,4 +191,55 @@ for (var i = 0; i < wrappers.length; i++) {
116191 input . trigger ( 'focusout' )
117192 assert . ok ( input . closest ( '.input-group' ) . find ( 'div.invalid-feedback' ) . length === 0 )
118193 } )
194+
195+ QUnit . test ( wrapper + ' - Validate date input' , function ( assert ) {
196+ const form = $ ( 'form#new_user' ) ;
197+ const select_year = form . find ( 'select#user_date_of_birth_1i' )
198+ const select_month = form . find ( 'select#user_date_of_birth_2i' )
199+ const select_day = form . find ( 'select#user_date_of_birth_3i' )
200+
201+ form [ 0 ] . ClientSideValidations . settings . html_settings . wrapper = wrapper
202+
203+ select_year . trigger ( 'focusout' )
204+ select_day . trigger ( 'focusout' )
205+
206+ assert . ok ( select_year . closest ( '.form-group' ) . hasClass ( 'form-group-invalid' ) )
207+ assert . ok ( select_month . closest ( '.form-group' ) . hasClass ( 'form-group-invalid' ) )
208+ assert . ok ( select_year . closest ( '.form-group' ) . find ( 'div.invalid-feedback' ) . length === 1 )
209+
210+ select_year . val ( 2025 ) . trigger ( 'change' ) . trigger ( 'focusout' )
211+
212+ assert . ok ( select_year . closest ( '.form-group' ) . hasClass ( 'form-group-invalid' ) )
213+ assert . ok ( select_year . closest ( '.form-group' ) . find ( 'div.invalid-feedback' ) . length === 1 )
214+
215+ select_month . val ( 1 ) . trigger ( 'change' ) . trigger ( 'focusout' )
216+ select_day . val ( 1 ) . trigger ( 'change' ) . trigger ( 'focusout' )
217+
218+ assert . notOk ( select_year . closest ( '.form-group' ) . hasClass ( 'form-group-invalid' ) )
219+ assert . ok ( select_year . closest ( '.form-group' ) . find ( 'div.invalid-feedback' ) . length === 0 )
220+ } )
221+
222+ QUnit . test ( wrapper + ' - Validate time input reusing server message' , function ( assert ) {
223+ const form = $ ( 'form#new_user' ) ;
224+ const select_hour = form . find ( 'select#user_time_of_birth_4i' )
225+ const select_minute = form . find ( 'select#user_time_of_birth_5i' )
226+
227+ form [ 0 ] . ClientSideValidations . settings . html_settings . wrapper = wrapper
228+
229+ assert . ok ( select_hour . closest ( '.form-group' ) . hasClass ( 'form-group-invalid' ) )
230+ assert . ok ( select_minute . closest ( '.form-group' ) . hasClass ( 'form-group-invalid' ) )
231+ assert . ok ( select_hour . closest ( '.form-group' ) . find ( 'div.invalid-feedback' ) . length === 1 )
232+
233+ select_hour . val ( 23 ) . trigger ( 'change' ) . trigger ( 'focusout' )
234+
235+ assert . ok ( select_hour . closest ( '.form-group' ) . hasClass ( 'form-group-invalid' ) )
236+ assert . ok ( select_minute . closest ( '.form-group' ) . hasClass ( 'form-group-invalid' ) )
237+ assert . ok ( select_hour . closest ( '.form-group' ) . find ( 'div.invalid-feedback' ) . length === 1 )
238+
239+ select_minute . val ( 55 ) . trigger ( 'change' ) . trigger ( 'focusout' )
240+
241+ assert . notOk ( select_hour . closest ( '.form-group' ) . hasClass ( 'form-group-invalid' ) )
242+ assert . ok ( select_hour . closest ( '.form-group' ) . find ( 'div.invalid-feedback' ) . length === 0 )
243+ } )
244+
119245}
0 commit comments