form_validation.js 15 KB

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