form.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // form
  2. test( "form elements check", function() {
  3. // deepEqual( item, test.value, 'group set/get tests' );
  4. $('#forms').append('<hr><h1>form elements check</h1><form id="form1"></form>')
  5. var el = $('#form1')
  6. var defaults = {
  7. input2: '123abc',
  8. password2: 'pw1234<l>',
  9. textarea2: 'lalu <l> lalu',
  10. select1: false,
  11. select2: true,
  12. selectmulti1: false,
  13. selectmulti2: [ false, true ],
  14. selectmultioption1: false,
  15. selectmultioption2: [ false, true ],
  16. }
  17. new App.ControllerForm({
  18. el: el,
  19. model: {
  20. configure_attributes: [
  21. { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: true, default: defaults['input1'] },
  22. { name: 'input2', display: 'Input2', tag: 'input', type: 'text', limit: 100, null: false, default: defaults['input2'] },
  23. { name: 'password1', display: 'Password1', tag: 'input', type: 'password', limit: 100, null: true, default: defaults['password1'] },
  24. { name: 'password2', display: 'Password2', tag: 'input', type: 'password', limit: 100, null: false, default: defaults['password2'] },
  25. { name: 'textarea1', display: 'Textarea1', tag: 'textarea', rows: 6, limit: 100, null: true, upload: true, default: defaults['textarea1'] },
  26. { name: 'textarea2', display: 'Textarea2', tag: 'textarea', rows: 6, limit: 100, null: false, upload: true, default: defaults['textarea2'] },
  27. { name: 'select1', display: 'Select1', tag: 'select', null: true, options: { true: 'internal', false: 'public' }, default: defaults['select1'] },
  28. { name: 'select2', display: 'Select2', tag: 'select', null: false, options: { true: 'internal', false: 'public' }, default: defaults['select2'] },
  29. { name: 'selectmulti1', display: 'SelectMulti1', tag: 'select', null: true, multiple: true, options: { true: 'internal', false: 'public' }, default: defaults['selectmulti1'] },
  30. { name: 'selectmulti2', display: 'SelectMulti2', tag: 'select', null: false, multiple: true, options: { true: 'internal', false: 'public' }, default: defaults['selectmulti2'] },
  31. { name: 'selectmultioption1', display: 'SelectMultiOption1', tag: 'select', null: true, multiple: true, options: [{ value: true, name: 'internal' }, { value: false, name: 'public' }], default: defaults['selectmultioption1'] },
  32. { name: 'selectmultioption2', display: 'SelectMultiOption2', tag: 'select', null: false, multiple: true, options: [{ value: true, name: 'A' }, { value: 1, name: 'B'}, { value: false, name: 'C' }], default: defaults['selectmultioption2'] },
  33. ]
  34. },
  35. autofocus: true
  36. });
  37. equal( el.find('[name="input1"]').val(), '', 'check input1 value')
  38. equal( el.find('[name="input1"]').prop('required'), false, 'check input1 required')
  39. // equal( el.find('[name="input1"]').is(":focus"), true, 'check input1 focus')
  40. equal( el.find('[name="input2"]').val(), '123abc', 'check input2 value')
  41. equal( el.find('[name="input2"]').prop('required'), true, 'check input2 required')
  42. equal( el.find('[name="input2"]').is(":focus"), false, 'check input2 focus')
  43. equal( el.find('[name="password1"]').val(), '', 'check password1 value')
  44. equal( el.find('[name="password1_confirm"]').val(), '', 'check password1 value')
  45. equal( el.find('[name="password1"]').prop('required'), false, 'check password1 required')
  46. equal( el.find('[name="password1"]').is(":focus"), false, 'check password1 focus')
  47. equal( el.find('[name="password2"]').val(), 'pw1234<l>', 'check password2 value')
  48. equal( el.find('[name="password2_confirm"]').val(), 'pw1234<l>', 'check password2 value')
  49. equal( el.find('[name="password2"]').prop('required'), true, 'check password2 required')
  50. equal( el.find('[name="password2"]').is(":focus"), false, 'check password2 focus')
  51. equal( el.find('[name="textarea1"]').val(), '', 'check textarea1 value')
  52. equal( el.find('[name="textarea1"]').prop('required'), false, 'check textarea1 required')
  53. equal( el.find('[name="textarea1"]').is(":focus"), false, 'check textarea1 focus')
  54. equal( el.find('[name="textarea2"]').val(), 'lalu <l> lalu', 'check textarea2 value')
  55. equal( el.find('[name="textarea2"]').prop('required'), true, 'check textarea2 required')
  56. equal( el.find('[name="textarea2"]').is(":focus"), false, 'check textarea2 focus')
  57. equal( el.find('[name="select1"]').val(), 'false', 'check select1 value')
  58. equal( el.find('[name="select1"]').prop('required'), false, 'check select1 required')
  59. equal( el.find('[name="select1"]').is(":focus"), false, 'check select1 focus')
  60. equal( el.find('[name="select2"]').val(), 'true', 'check select2 value')
  61. equal( el.find('[name="select2"]').prop('required'), true, 'check select2 required')
  62. equal( el.find('[name="select2"]').is(":focus"), false, 'check select2 focus')
  63. equal( el.find('[name="selectmulti1"]').val(), 'false', 'check selectmulti1 value')
  64. equal( el.find('[name="selectmulti1"]').prop('required'), false, 'check selectmulti1 required')
  65. equal( el.find('[name="selectmulti1"]').is(":focus"), false, 'check selectmulti1 focus')
  66. equal( el.find('[name="selectmulti2"]').val()[0], 'true', 'check selectmulti2 value')
  67. equal( el.find('[name="selectmulti2"]').val()[1], 'false', 'check selectmulti2 value')
  68. equal( el.find('[name="selectmulti2"]').prop('required'), true, 'check selectmulti2 required')
  69. equal( el.find('[name="selectmulti2"]').is(":focus"), false, 'check selectmulti2 focus')
  70. });
  71. test( "form params check", function() {
  72. // deepEqual( item, test.value, 'group set/get tests' );
  73. $('#forms').append('<hr><h1>form params check</h1><form id="form2"></form>')
  74. var el = $('#form2')
  75. var defaults = {
  76. input2: '123abc',
  77. password2: 'pw1234<l>',
  78. textarea2: 'lalu <l> lalu',
  79. select1: false,
  80. select2: true,
  81. selectmulti1: false,
  82. selectmulti2: [ false, true ],
  83. selectmultioption1: false,
  84. selectmultioption2: [ false, true ],
  85. autocompletion2: 'id2',
  86. autocompletion2_autocompletion_value_shown: 'value2',
  87. }
  88. new App.ControllerForm({
  89. el: el,
  90. model: {
  91. configure_attributes: [
  92. { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: true },
  93. { name: 'input2', display: 'Input2', tag: 'input', type: 'text', limit: 100, null: false },
  94. { name: 'password1', display: 'Password1', tag: 'input', type: 'password', limit: 100, null: true },
  95. { name: 'password2', display: 'Password2', tag: 'input', type: 'password', limit: 100, null: false },
  96. { name: 'textarea1', display: 'Textarea1', tag: 'textarea', rows: 6, limit: 100, null: true, upload: true },
  97. { name: 'textarea2', display: 'Textarea2', tag: 'textarea', rows: 6, limit: 100, null: false, upload: true },
  98. { name: 'select1', display: 'Select1', tag: 'select', null: true, options: { true: 'internal', false: 'public' } },
  99. { name: 'select2', display: 'Select2', tag: 'select', null: false, options: { true: 'internal', false: 'public' } },
  100. { name: 'selectmulti1', display: 'SelectMulti1', tag: 'select', null: true, multiple: true, options: { true: 'internal', false: 'public' } },
  101. { name: 'selectmulti2', display: 'SelectMulti2', tag: 'select', null: false, multiple: true, options: { true: 'internal', false: 'public' } },
  102. { name: 'selectmultioption1', display: 'SelectMultiOption1', tag: 'select', null: true, multiple: true, options: [{ value: true, name: 'internal' }, { value: false, name: 'public' }] },
  103. { name: 'selectmultioption2', display: 'SelectMultiOption2', tag: 'select', null: false, multiple: true, options: [{ value: true, name: 'A' }, { value: 1, name: 'B'}, { value: false, name: 'C' }] },
  104. { 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 },
  105. { name: 'autocompletion2', display: 'AutoCompletion2', tag: 'autocompletion', null: false, options: { true: 'internal', false: 'public' }, source: [ { label: "Choice1", value: "value1", id: "id1" }, { label: "Choice2", value: "value2", id: "id2" }, ], minLength: 1 },
  106. ],
  107. },
  108. params: defaults,
  109. autofocus: true
  110. });
  111. equal( el.find('[name="input1"]').val(), '', 'check input1 value')
  112. equal( el.find('[name="input1"]').prop('required'), false, 'check input1 required')
  113. // equal( el.find('[name="input1"]').is(":focus"), true, 'check input1 focus')
  114. equal( el.find('[name="input2"]').val(), '123abc', 'check input2 value')
  115. equal( el.find('[name="input2"]').prop('required'), true, 'check input2 required')
  116. equal( el.find('[name="input2"]').is(":focus"), false, 'check input2 focus')
  117. equal( el.find('[name="password1"]').val(), '', 'check password1 value')
  118. equal( el.find('[name="password1_confirm"]').val(), '', 'check password1 value')
  119. equal( el.find('[name="password1"]').prop('required'), false, 'check password1 required')
  120. equal( el.find('[name="password1"]').is(":focus"), false, 'check password1 focus')
  121. equal( el.find('[name="password2"]').val(), 'pw1234<l>', 'check password2 value')
  122. equal( el.find('[name="password2_confirm"]').val(), 'pw1234<l>', 'check password2 value')
  123. equal( el.find('[name="password2"]').prop('required'), true, 'check password2 required')
  124. equal( el.find('[name="password2"]').is(":focus"), false, 'check password2 focus')
  125. equal( el.find('[name="textarea1"]').val(), '', 'check textarea1 value')
  126. equal( el.find('[name="textarea1"]').prop('required'), false, 'check textarea1 required')
  127. equal( el.find('[name="textarea1"]').is(":focus"), false, 'check textarea1 focus')
  128. equal( el.find('[name="textarea2"]').val(), 'lalu <l> lalu', 'check textarea2 value')
  129. equal( el.find('[name="textarea2"]').prop('required'), true, 'check textarea2 required')
  130. equal( el.find('[name="textarea2"]').is(":focus"), false, 'check textarea2 focus')
  131. equal( el.find('[name="select1"]').val(), 'false', 'check select1 value')
  132. equal( el.find('[name="select1"]').prop('required'), false, 'check select1 required')
  133. equal( el.find('[name="select1"]').is(":focus"), false, 'check select1 focus')
  134. equal( el.find('[name="select2"]').val(), 'true', 'check select2 value')
  135. equal( el.find('[name="select2"]').prop('required'), true, 'check select2 required')
  136. equal( el.find('[name="select2"]').is(":focus"), false, 'check select2 focus')
  137. equal( el.find('[name="selectmulti1"]').val(), 'false', 'check selectmulti1 value')
  138. equal( el.find('[name="selectmulti1"]').prop('required'), false, 'check selectmulti1 required')
  139. equal( el.find('[name="selectmulti1"]').is(":focus"), false, 'check selectmulti1 focus')
  140. equal( el.find('[name="selectmulti2"]').val()[0], 'true', 'check selectmulti2 value')
  141. equal( el.find('[name="selectmulti2"]').val()[1], 'false', 'check selectmulti2 value')
  142. equal( el.find('[name="selectmulti2"]').prop('required'), true, 'check selectmulti2 required')
  143. equal( el.find('[name="selectmulti2"]').is(":focus"), false, 'check selectmulti2 focus')
  144. });
  145. test( "form defaults + params check", function() {
  146. // deepEqual( item, test.value, 'group set/get tests' );
  147. // mix default and params -> check it -> add note
  148. // test auto completion
  149. // show/hide fields base on field values -> bind changed event
  150. // form validation
  151. // form params check
  152. // add signature only if form_state is empty
  153. $('#forms').append('<hr><h1>form defaults + params check</h1><form id="form3"></form>')
  154. var el = $('#form3')
  155. var defaults = {
  156. input1: '',
  157. password2: 'pw1234<l>',
  158. textarea2: 'lalu <l> lalu',
  159. select2: false,
  160. selectmulti2: [ false, true ],
  161. selectmultioption1: false,
  162. }
  163. new App.ControllerForm({
  164. el: el,
  165. model: {
  166. configure_attributes: [
  167. { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: true, default: 'some not used default' },
  168. { name: 'input2', display: 'Input2', tag: 'input', type: 'text', limit: 100, null: true, default: 'some used default' },
  169. { name: 'password1', display: 'Password1', tag: 'input', type: 'password', limit: 100, null: false, default: 'some used pass' },
  170. { name: 'password2', display: 'Password2', tag: 'input', type: 'password', limit: 100, null: false, default: 'some not used pass' },
  171. { name: 'textarea1', display: 'Textarea1', tag: 'textarea', rows: 6, limit: 100, null: false, upload: true, default: 'some used text' },
  172. { name: 'textarea2', display: 'Textarea2', tag: 'textarea', rows: 6, limit: 100, null: false, upload: true, default: 'some not used text' },
  173. { name: 'select1', display: 'Select1', tag: 'select', null: true, options: { true: 'internal', false: 'public' }, default: false},
  174. { name: 'select2', display: 'Select2', tag: 'select', null: true, options: { true: 'internal', false: 'public' }, default: true },
  175. { name: 'selectmulti2', display: 'SelectMulti2', tag: 'select', null: false, multiple: true, options: { true: 'internal', false: 'public' }, default: [] },
  176. { name: 'selectmultioption1', display: 'SelectMultiOption1', tag: 'select', null: true, multiple: true, options: [{ value: true, name: 'internal' }, { value: false, name: 'public' }], default: true },
  177. ],
  178. },
  179. params: defaults,
  180. autofocus: true
  181. });
  182. equal( el.find('[name="input1"]').val(), '', 'check input1 value')
  183. equal( el.find('[name="input1"]').prop('required'), false, 'check input1 required')
  184. // equal( el.find('[name="input1"]').is(":focus"), true, 'check input1 focus')
  185. equal( el.find('[name="input2"]').val(), 'some used default', 'check input2 value')
  186. equal( el.find('[name="input2"]').prop('required'), false, 'check input2 required')
  187. equal( el.find('[name="password1"]').val(), 'some used pass', 'check password1 value')
  188. equal( el.find('[name="password1_confirm"]').val(), 'some used pass', 'check password1 value')
  189. equal( el.find('[name="password1"]').prop('required'), true, 'check password1 required')
  190. equal( el.find('[name="password1"]').is(":focus"), false, 'check password1 focus')
  191. equal( el.find('[name="password2"]').val(), 'pw1234<l>', 'check password2 value')
  192. equal( el.find('[name="password2_confirm"]').val(), 'pw1234<l>', 'check password2 value')
  193. equal( el.find('[name="password2"]').prop('required'), true, 'check password2 required')
  194. equal( el.find('[name="password2"]').is(":focus"), false, 'check password2 focus')
  195. equal( el.find('[name="textarea1"]').val(), 'some used text', 'check textarea1 value')
  196. equal( el.find('[name="textarea1"]').prop('required'), true, 'check textarea1 required')
  197. equal( el.find('[name="textarea1"]').is(":focus"), false, 'check textarea1 focus')
  198. equal( el.find('[name="textarea2"]').val(), 'lalu <l> lalu', 'check textarea2 value')
  199. equal( el.find('[name="textarea2"]').prop('required'), true, 'check textarea2 required')
  200. equal( el.find('[name="textarea2"]').is(":focus"), false, 'check textarea2 focus')
  201. equal( el.find('[name="select1"]').val(), 'false', 'check select1 value')
  202. equal( el.find('[name="select1"]').prop('required'), false, 'check select1 required')
  203. equal( el.find('[name="select1"]').is(":focus"), false, 'check select1 focus')
  204. equal( el.find('[name="select2"]').val(), 'false', 'check select2 value')
  205. equal( el.find('[name="select2"]').prop('required'), false, 'check select2 required')
  206. equal( el.find('[name="select2"]').is(":focus"), false, 'check select2 focus')
  207. equal( el.find('[name="selectmulti2"]').val()[0], 'true', 'check selectmulti2 value')
  208. equal( el.find('[name="selectmulti2"]').val()[1], 'false', 'check selectmulti2 value')
  209. equal( el.find('[name="selectmulti2"]').prop('required'), true, 'check selectmulti2 required')
  210. equal( el.find('[name="selectmulti2"]').is(":focus"), false, 'check selectmulti2 focus')
  211. });
  212. test( "form dependend fields check", function() {
  213. // deepEqual( item, test.value, 'group set/get tests' );
  214. // mix default and params -> check it -> add note
  215. // test auto completion
  216. // show/hide fields base on field values -> bind changed event
  217. // form validation
  218. // form params check
  219. // add signature only if form_state is empty
  220. $('#forms').append('<hr><h1>form dependend fields check</h1><form id="form4"></form>')
  221. var el = $('#form4')
  222. var defaults = {
  223. input1: '',
  224. select2: false,
  225. selectmulti2: [ false, true ],
  226. selectmultioption1: false,
  227. }
  228. new App.ControllerForm({
  229. el: el,
  230. model: {
  231. configure_attributes: [
  232. { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: true, default: 'some not used default' },
  233. { name: 'input2', display: 'Input2', tag: 'input', type: 'text', limit: 100, null: true, default: 'some used default' },
  234. { name: 'input3', display: 'Input3', tag: 'input', type: 'text', limit: 100, null: true, hide: true, default: 'some used default' },
  235. { name: 'select1', display: 'Select1', tag: 'select', null: true, options: { true: 'internal', false: 'public' }, default: false},
  236. { name: 'select2', display: 'Select2', tag: 'select', null: true, options: { true: 'internal', false: 'public' }, default: true },
  237. { name: 'selectmulti2', display: 'SelectMulti2', tag: 'select', null: false, multiple: true, options: { true: 'internal', false: 'public' }, default: [] },
  238. { name: 'selectmultioption1', display: 'SelectMultiOption1', tag: 'select', null: true, multiple: true, options: [{ value: true, name: 'internal' }, { value: false, name: 'public' }], default: true },
  239. ],
  240. },
  241. params: defaults,
  242. dependency: [
  243. {
  244. bind: {
  245. name: 'select1',
  246. value: ["true"]
  247. },
  248. change: {
  249. name: 'input2',
  250. action: 'hide'
  251. },
  252. },
  253. {
  254. bind: {
  255. name: 'select1',
  256. value: ["false"]
  257. },
  258. change: {
  259. name: 'input2',
  260. action: 'show'
  261. },
  262. },
  263. {
  264. bind: {
  265. name: 'select1',
  266. value: ["true"]
  267. },
  268. change: {
  269. name: 'input3',
  270. action: 'show'
  271. },
  272. },
  273. {
  274. bind: {
  275. name: 'select1',
  276. value: ["false"]
  277. },
  278. change: {
  279. name: 'input3',
  280. action: 'hide'
  281. },
  282. }
  283. ],
  284. autofocus: true
  285. });
  286. equal( el.find('[name="input1"]').val(), '', 'check input1 value')
  287. equal( el.find('[name="input1"]').prop('required'), false, 'check input1 required')
  288. // equal( el.find('[name="input1"]').is(":focus"), true, 'check input1 focus')
  289. equal( el.find('[name="input2"]').val(), 'some used default', 'check input2 value')
  290. equal( el.find('[name="input2"]').prop('required'), false, 'check input2 required')
  291. equal( el.find('[name="input3"]').val(), 'some used default', 'check input3 value')
  292. equal( el.find('[name="input3"]').prop('required'), false, 'check input3 required')
  293. equal( el.find('[name="select1"]').val(), 'false', 'check select1 value')
  294. equal( el.find('[name="select1"]').prop('required'), false, 'check select1 required')
  295. equal( el.find('[name="select1"]').is(":focus"), false, 'check select1 focus')
  296. equal( el.find('[name="select2"]').val(), 'false', 'check select2 value')
  297. equal( el.find('[name="select2"]').prop('required'), false, 'check select2 required')
  298. equal( el.find('[name="select2"]').is(":focus"), false, 'check select2 focus')
  299. equal( el.find('[name="selectmulti2"]').val()[0], 'true', 'check selectmulti2 value')
  300. equal( el.find('[name="selectmulti2"]').val()[1], 'false', 'check selectmulti2 value')
  301. equal( el.find('[name="selectmulti2"]').prop('required'), true, 'check selectmulti2 required')
  302. equal( el.find('[name="selectmulti2"]').is(":focus"), false, 'check selectmulti2 focus')
  303. var params = App.ControllerForm.params( el )
  304. var test_params = {
  305. input1: "",
  306. input2: "some used default",
  307. select1: "false",
  308. select2: "false",
  309. selectmulti2: [ "true", "false" ],
  310. selectmultioption1: "false"
  311. }
  312. deepEqual( params, test_params, 'form param check' );
  313. el.find('[name="select1"]').val('true')
  314. el.find('[name="select1"]').trigger('change')
  315. params = App.ControllerForm.params( el )
  316. test_params = {
  317. input1: "",
  318. input3: "some used default",
  319. select1: "true",
  320. select2: "false",
  321. selectmulti2: [ "true", "false" ],
  322. selectmultioption1: "false"
  323. }
  324. deepEqual( params, test_params, 'form param check' );
  325. });