form_validation.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. QUnit.test('form validation check', assert => {
  2. App.i18n.set('en-us')
  3. $('#forms').append('<hr><h1>form params check</h1><form id="form1"></form>')
  4. var el = $('#form1')
  5. var defaults = {}
  6. var form = new App.ControllerForm({
  7. el: el,
  8. model: {
  9. configure_attributes: [
  10. { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: false },
  11. { name: 'password1', display: 'Password1', tag: 'input', type: 'password', limit: 100, null: false },
  12. { name: 'textarea1', display: 'Textarea1', tag: 'textarea', rows: 6, limit: 100, null: false, upload: true },
  13. { name: 'select1', display: 'Select1', tag: 'select', null: false, nulloption: true, options: { true: 'internal', false: 'public' } },
  14. { name: 'selectmulti1', display: 'SelectMulti1', tag: 'select', null: false, nulloption: true, multiple: true, options: { true: 'internal', false: 'public' } },
  15. { name: 'autocompletion1', display: 'AutoCompletion1', tag: 'autocompletion', null: false, options: { true: 'internal', false: 'public' }, source: [ { label: "Choice1", value: "value1", id: "id1" }, { label: "Choice2", value: "value2", id: "id2" }, ], minLength: 1 },
  16. { name: 'richtext1', display: 'Richtext1', tag: 'richtext', maxlength: 100, null: false, type: 'richtext', multiline: true, upload: true, default: defaults['richtext1'] },
  17. { name: 'datetime1', display: 'Datetime1', tag: 'datetime', null: false, default: defaults['datetime1'] },
  18. { name: 'date1', display: 'Date1', tag: 'date', null: false, default: defaults['date1'] },
  19. { name: 'active1', display: 'Active1', tag: 'active', default: defaults['active1'] },
  20. ],
  21. },
  22. params: defaults,
  23. });
  24. assert.equal(el.find('[name="input1"]').val(), '', 'check input1 value')
  25. assert.equal(el.find('[name="input1"]').prop('required'), true, 'check input1 required')
  26. // assert.equal(el.find('[name="input1"]').is(":focus"), true, 'check input1 focus')
  27. assert.equal(el.find('[name="password1"]').val(), '', 'check password1 value')
  28. assert.equal(el.find('[name="password1_confirm"]').val(), '', 'check password1 value')
  29. assert.equal(el.find('[name="password1"]').prop('required'), true, 'check password1 required')
  30. assert.equal(el.find('[name="textarea1"]').val(), '', 'check textarea1 value')
  31. assert.equal(el.find('[name="textarea1"]').prop('required'), true, 'check textarea1 required')
  32. assert.equal(el.find('[name="select1"]').val(), '', 'check select1 value')
  33. assert.equal(el.find('[name="select1"]').prop('required'), true, 'check select1 required')
  34. assert.equal(el.find('[name="selectmulti1"]').val(), '', 'check selectmulti1 value')
  35. assert.equal(el.find('[name="selectmulti1"]').prop('required'), true, 'check selectmulti1 required')
  36. assert.equal(el.find('[name="autocompletion1"]').val(), '', 'check autocompletion1 value')
  37. assert.equal(el.find('[name="autocompletion1"]').prop('required'), true, 'check autocompletion1 required')
  38. assert.equal(el.find('[data-name="richtext1"]').val(), '', 'check richtext1 value')
  39. //equal(el.find('[data-name="richtext1"]').prop('required'), true, 'check richtext1 required')
  40. params = App.ControllerForm.params(el)
  41. errors = form.validate(params)
  42. test_errors = {
  43. input1: 'is required',
  44. password1: 'is required',
  45. textarea1: 'is required',
  46. select1: 'is required',
  47. selectmulti1: 'is required',
  48. autocompletion1: 'is required',
  49. richtext1: 'is required',
  50. datetime1: 'is required',
  51. date1: 'is required',
  52. }
  53. assert.deepEqual(errors, test_errors, 'validation errors check')
  54. App.ControllerForm.validate({ errors: errors, form: el })
  55. assert.equal(el.find('[name="input1"]').closest('.form-group').hasClass('has-error'), true, 'check input1 has-error')
  56. assert.equal(el.find('[name="input1"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check input1 error message')
  57. assert.equal(el.find('[name="password1"]').closest('.form-group').hasClass('has-error'), true, 'check password1 has-error')
  58. assert.equal(el.find('[name="password1"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check password1 error message')
  59. assert.equal(el.find('[name="textarea1"]').closest('.form-group').hasClass('has-error'), true, 'check textarea1 has-error')
  60. assert.equal(el.find('[name="textarea1"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check textarea1 error message')
  61. assert.equal(el.find('[name="select1"]').closest('.form-group').hasClass('has-error'), true, 'check select1 has-error')
  62. assert.equal(el.find('[name="select1"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check select1 error message')
  63. assert.equal(el.find('[name="selectmulti1"]').closest('.form-group').hasClass('has-error'), true, 'check selectmulti1 has-error')
  64. assert.equal(el.find('[name="selectmulti1"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check selectmulti1 error message')
  65. assert.equal(el.find('[name="autocompletion1"]').closest('.form-group').hasClass('has-error'), true, 'check autocompletion1 has-error')
  66. assert.equal(el.find('[name="autocompletion1"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check autocompletion1 error message')
  67. assert.equal(el.find('[data-name="richtext1"]').closest('.form-group').hasClass('has-error'), true, 'check richtext1 has-error')
  68. assert.equal(el.find('[data-name="richtext1"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check richtext1 error message')
  69. assert.equal(el.find('[data-name="datetime1"]').closest('.form-group').hasClass('has-error'), true, 'check datetime1 has-error')
  70. assert.equal(el.find('[data-name="datetime1"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check datetime1 error message')
  71. assert.equal(el.find('[data-name="date1"]').closest('.form-group').hasClass('has-error'), true, 'check date1 has-error')
  72. assert.equal(el.find('[data-name="date1"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check date1 error message')
  73. });
  74. QUnit.test('datetime validation check', assert => {
  75. App.i18n.set('en-us')
  76. $('#forms').append('<hr><h1>datetime validation check</h1><form id="form2"></form>')
  77. var el = $('#form2')
  78. var defaults = {}
  79. var form = new App.ControllerForm({
  80. el: el,
  81. model: {
  82. configure_attributes: [
  83. { name: 'datetime1', display: 'Datetime1', tag: 'datetime', null: false, default: defaults['datetime1'] },
  84. ],
  85. },
  86. params: defaults,
  87. });
  88. // check params
  89. params = App.ControllerForm.params(el)
  90. test_params = {
  91. datetime1: null,
  92. }
  93. assert.deepEqual(params, test_params, 'params check')
  94. errors = form.validate(params)
  95. test_errors = {
  96. datetime1: 'is required',
  97. }
  98. assert.deepEqual(errors, test_errors, 'validation errors check')
  99. App.ControllerForm.validate({ errors: errors, form: el })
  100. assert.equal(el.find('[data-name="datetime1"]').closest('.form-group').hasClass('has-error'), true, 'check datetime1 has-error')
  101. assert.equal(el.find('[data-name="datetime1"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check datetime1 error message')
  102. //equal(el.find('[data-name="datetime1"]').closest('.form-group').find('.help-inline').text(), '', 'check datetime1 error message')
  103. // set new values
  104. el.find('[data-name="datetime1"] [data-item="date"]').val('01/01/2015').trigger('blur')
  105. el.find('[data-name="datetime1"] [data-item="date"]').datepicker('setDate')
  106. el.find('[data-name="datetime1"] [data-item="time"]').val('12:42').trigger('blur')
  107. el.find('[data-name="datetime1"] [data-item="time"]').trigger('change')
  108. // check params
  109. timeStamp = new Date( Date.parse('2015-01-01T12:42:00.000Z') )
  110. timeStamp.setMinutes( timeStamp.getMinutes() + timeStamp.getTimezoneOffset() )
  111. params = App.ControllerForm.params(el)
  112. test_params = {
  113. datetime1: timeStamp.toISOString(),
  114. }
  115. assert.deepEqual(params, test_params, 'params check')
  116. // check errors
  117. errors = form.validate(params)
  118. test_errors = undefined
  119. assert.deepEqual(errors, test_errors, 'validation errors check')
  120. App.ControllerForm.validate({ errors: errors, form: el })
  121. assert.equal(el.find('[data-name="datetime1"]').closest('.form-group').hasClass('has-error'), false, 'check datetime1 has-error')
  122. assert.equal(el.find('[data-name="datetime1"]').closest('.form-group').find('.help-inline').text(), '', 'check datetime1 error message')
  123. el.find('[data-name="datetime1"] [data-item="date"]').val('').trigger('blur')
  124. el.find('[data-name="datetime1"] [data-item="date"]').datepicker('setDate')
  125. el.find('[data-name="datetime1"] [data-item="time"]').val('12:42').trigger('blur')
  126. el.find('[data-name="datetime1"] [data-item="time"]').trigger('change')
  127. assert.equal(el.find('[data-name="datetime1"]').closest('.form-group').hasClass('has-error'), true )
  128. App.i18n.set('de-de')
  129. params = App.ControllerForm.params(el)
  130. errors = form.validate(params)
  131. test_errors = {
  132. datetime1: 'is required',
  133. }
  134. assert.deepEqual(errors, test_errors, 'validation errors check')
  135. App.ControllerForm.validate({ errors: errors, form: el })
  136. assert.equal(el.find('[data-name="datetime1"]').closest('.form-group').hasClass('has-error'), true, 'check datetime1 no has-error')
  137. assert.equal(el.find('[data-name="datetime1"]').closest('.form-group').find('.help-inline').text(), 'ist erforderlich', 'check datetime1 error message')
  138. });
  139. QUnit.test('date validation check', assert => {
  140. App.i18n.set('en-us')
  141. $('#forms').append('<hr><h1>date validation check</h1><form id="form3"></form>')
  142. var el = $('#form3')
  143. var defaults = {}
  144. var form = new App.ControllerForm({
  145. el: el,
  146. model: {
  147. configure_attributes: [
  148. { name: 'date2', display: 'Date2', tag: 'date', null: false, default: defaults['time1'] },
  149. ],
  150. },
  151. params: defaults,
  152. });
  153. params = App.ControllerForm.params(el)
  154. // check params
  155. params = App.ControllerForm.params(el)
  156. test_params = {
  157. date2: null,
  158. }
  159. assert.deepEqual(params, test_params, 'params check')
  160. errors = form.validate(params)
  161. test_errors = {
  162. date2: 'is required',
  163. }
  164. assert.deepEqual(errors, test_errors, 'validation errors check')
  165. App.ControllerForm.validate({ errors: errors, form: el })
  166. assert.equal(el.find('[data-name="date2"]').closest('.form-group').hasClass('has-error'), true, 'check date2 has-error')
  167. assert.equal(el.find('[data-name="date2"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check date2 error message')
  168. // set new values
  169. el.find('[data-name="date2"] [data-item="date"]').val('01/01/2015').trigger('blur')
  170. el.find('[data-name="date2"] [data-item="date"]').datepicker('setDate')
  171. el.find('[data-name="date2"] [data-item="date"]').trigger('change')
  172. // check params
  173. params = App.ControllerForm.params(el)
  174. test_params = {
  175. date2: '2015-01-01',
  176. }
  177. assert.deepEqual(params, test_params, 'params check')
  178. // check errors
  179. errors = form.validate(params)
  180. test_errors = undefined
  181. assert.deepEqual(errors, test_errors, 'validation errors check')
  182. App.ControllerForm.validate({ errors: errors, form: el })
  183. assert.equal(el.find('[data-name="date2"]').closest('.form-group').hasClass('has-error'), false, 'check date1 has-error')
  184. assert.equal(el.find('[data-name="date2"]').closest('.form-group').find('.help-inline').text(), '', 'check date1 error message')
  185. // set invalid values
  186. el.find('[data-name="date2"] [data-item="date"]').val('').trigger('blur')
  187. el.find('[data-name="date2"] [data-item="date"]').datepicker('setDate')
  188. el.find('[data-name="date2"] [data-item="date"]').trigger('change')
  189. assert.equal(el.find('[data-name="date2"]').closest('.form-group').hasClass('has-error'), true, 'check date2 has-error')
  190. // check params
  191. params = App.ControllerForm.params(el)
  192. test_params = {
  193. date2: null,
  194. }
  195. assert.deepEqual(params, test_params, 'params check')
  196. // check errors
  197. errors = form.validate(params)
  198. test_errors = {
  199. date2: 'is required',
  200. }
  201. assert.deepEqual(errors, test_errors, 'validation errors check')
  202. App.ControllerForm.validate({ errors: errors, form: el })
  203. assert.equal(el.find('[data-name="date2"]').closest('.form-group').hasClass('has-error'), true, 'check date2 has-error')
  204. assert.equal(el.find('[data-name="date2"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check date2 error message')
  205. });
  206. QUnit.test( "datetime selector check", assert => {
  207. App.i18n.set('en-us')
  208. $('#forms').append('<hr><h1>datetime selector check</h1><form id="form4"></form>')
  209. var el = $('#form4')
  210. var defaults = {}
  211. var form = new App.ControllerForm({
  212. el: el,
  213. model: {
  214. configure_attributes: [
  215. { name: 'datetime1', display: 'Datetime1', tag: 'datetime', null: false, default: defaults['datetime1'] },
  216. { name: 'datetime2', display: 'Datetime2', tag: 'datetime', null: false, default: defaults['datetime2'] },
  217. ],
  218. },
  219. params: defaults,
  220. });
  221. // check params
  222. params = App.ControllerForm.params(el)
  223. test_params = {
  224. datetime1: null,
  225. datetime2: null,
  226. }
  227. assert.deepEqual(params, test_params, 'params check')
  228. var timeStamp1 = new Date()
  229. timeStamp1.setMinutes(0)
  230. timeStamp1.setSeconds(0)
  231. timeStamp1.setMilliseconds(0)
  232. timeStamp1.setHours(8)
  233. el.find('[data-name="datetime1"] .js-datepicker').datepicker('setDate', timeStamp1)
  234. el.find('[data-name="datetime1"] .js-datepicker').trigger('blur')
  235. // check params
  236. params = App.ControllerForm.params(el)
  237. test_params = {
  238. datetime1: timeStamp1.toISOString(),
  239. datetime2: null,
  240. }
  241. assert.deepEqual(params, test_params, 'params check')
  242. el.find('[data-name="datetime1"] .js-timepicker[data-item="time"]').val('9:00')
  243. el.find('[data-name="datetime1"] .js-timepicker[data-item="time"]').trigger('blur')
  244. timeStamp1.setHours(9)
  245. // check params
  246. params = App.ControllerForm.params(el)
  247. test_params = {
  248. datetime1: timeStamp1.toISOString(),
  249. datetime2: null,
  250. }
  251. assert.deepEqual(params, test_params, 'params check')
  252. var timeStamp2 = new Date()
  253. timeStamp2.setMinutes(0)
  254. timeStamp2.setSeconds(0)
  255. timeStamp2.setMilliseconds(0)
  256. timeStamp2.setHours(22)
  257. el.find('[data-name="datetime2"] .js-datepicker').datepicker('setDate', timeStamp2)
  258. el.find('[data-name="datetime2"] .js-datepicker').trigger('blur')
  259. el.find('[data-name="datetime2"] .js-timepicker[data-item="time"]').val(timeStamp2.getHours() + ':00')
  260. el.find('[data-name="datetime2"] .js-timepicker[data-item="time"]').trigger('blur')
  261. // check params
  262. params = App.ControllerForm.params(el)
  263. test_params = {
  264. datetime1: timeStamp1.toISOString(),
  265. datetime2: timeStamp2.toISOString(),
  266. }
  267. assert.deepEqual(params, test_params, 'params check')
  268. // Regression test for issue #2173 - Invalid date causes errors
  269. el.find('[data-name="datetime1"] .js-datepicker').datepicker('setDate', '01/01/99999')
  270. el.find('[data-name="datetime1"] .js-datepicker').datepicker('setDate', '01/01/1ABCDEFG')
  271. el.find('[data-name="datetime1"] .js-datepicker').datepicker('setDate', '01/01/1äöüß')
  272. });
  273. QUnit.test( "date selector check", assert => {
  274. $('#forms').append('<hr><h1>date selector check</h1><form id="form5"></form>')
  275. var el = $('#form5')
  276. var defaults = {}
  277. var form = new App.ControllerForm({
  278. el: el,
  279. model: {
  280. configure_attributes: [
  281. { name: 'date3', display: 'Datet1', tag: 'date', null: false, default: defaults['date3'] },
  282. ],
  283. },
  284. params: defaults,
  285. });
  286. // check params
  287. params = App.ControllerForm.params(el)
  288. test_params = {
  289. date3: null,
  290. }
  291. assert.deepEqual(params, test_params, 'params check')
  292. timeStamp = new Date()
  293. el.find('.js-datepicker').datepicker('setDate', timeStamp)
  294. el.find('.js-datepicker').trigger('blur')
  295. // check params
  296. format = function (number) {
  297. if (parseInt(number) < 10 ) {
  298. number = '0' + number.toString()
  299. }
  300. return number
  301. }
  302. currentTime = timeStamp.getFullYear() + '-' + format(timeStamp.getMonth()+1) + '-' + format(timeStamp.getDate())
  303. params = App.ControllerForm.params(el)
  304. test_params = {
  305. date3: currentTime,
  306. }
  307. assert.deepEqual(params, test_params, 'params check')
  308. });