PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/error-placement.js

https://github.com/timbeadle/jquery-validation
JavaScript | 312 lines | 265 code | 44 blank | 3 comment | 0 complexity | a1ef24ca373bb52b8356bd601072230a MD5 | raw file
  1. module( "placement" );
  2. test( "elements() order", function() {
  3. var container = $( "#orderContainer" ),
  4. v = $( "#elementsOrder" ).validate({
  5. errorLabelContainer: container,
  6. wrap: "li"
  7. });
  8. deepEqual(
  9. v.elements().map( function() {
  10. return $( this ).attr( "id" );
  11. }).get(),
  12. [
  13. "order1",
  14. "order2",
  15. "order3",
  16. "order4",
  17. "order5",
  18. "order6"
  19. ],
  20. "elements must be in document order"
  21. );
  22. v.form();
  23. deepEqual(
  24. container.children().map( function() {
  25. return $( this ).attr( "id" );
  26. }).get(),
  27. [
  28. "order1-error",
  29. "order2-error",
  30. "order3-error",
  31. "order4-error",
  32. "order5-error",
  33. "order6-error"
  34. ],
  35. "labels in error container must be in document order"
  36. );
  37. });
  38. test( "error containers, simple", function() {
  39. expect( 14 );
  40. var container = $( "#simplecontainer" ),
  41. v = $( "#form" ).validate({
  42. errorLabelContainer: container,
  43. showErrors: function() {
  44. container.find( "h3" ).html( jQuery.validator.format( "There are {0} errors in your form.", this.size()) );
  45. this.defaultShowErrors();
  46. }
  47. });
  48. v.prepareForm();
  49. ok( v.valid(), "form is valid" );
  50. equal( 0, container.find( ".error:not(input)" ).length, "There should be no error labels" );
  51. equal( "", container.find( "h3" ).html() );
  52. v.prepareForm();
  53. v.errorList = [
  54. {
  55. message: "bar",
  56. element: {
  57. name: "foo"
  58. }
  59. },
  60. {
  61. message: "necessary",
  62. element: {
  63. name: "required"
  64. }
  65. }
  66. ];
  67. ok( !v.valid(), "form is not valid after adding errors manually" );
  68. v.showErrors();
  69. equal( container.find( ".error:not(input)" ).length, 2, "There should be two error labels" );
  70. ok( container.is( ":visible" ), "Check that the container is visible" );
  71. container.find( ".error:not(input)" ).each(function() {
  72. ok( $( this ).is( ":visible" ), "Check that each label is visible" );
  73. });
  74. equal( "There are 2 errors in your form.", container.find( "h3" ).html() );
  75. v.prepareForm();
  76. ok( v.valid(), "form is valid after a reset" );
  77. v.showErrors();
  78. equal( container.find( ".error:not(input)" ).length, 2, "There should still be two error labels" );
  79. ok( container.is( ":hidden" ), "Check that the container is hidden" );
  80. container.find( ".error:not(input)" ).each(function() {
  81. ok( $( this ).is( ":hidden" ), "Check that each label is hidden" );
  82. });
  83. });
  84. test( "error containers, with labelcontainer I", function() {
  85. expect( 16 );
  86. var container = $( "#container" ),
  87. labelcontainer = $( "#labelcontainer" ),
  88. v = $( "#form" ).validate({
  89. errorContainer: container,
  90. errorLabelContainer: labelcontainer,
  91. wrapper: "li"
  92. });
  93. ok( v.valid(), "form is valid" );
  94. equal( 0, container.find( ".error:not(input)" ).length, "There should be no error labels in the container" );
  95. equal( 0, labelcontainer.find( ".error:not(input)" ).length, "There should be no error labels in the labelcontainer" );
  96. equal( 0, labelcontainer.find( "li" ).length, "There should be no lis labels in the labelcontainer" );
  97. v.errorList = [
  98. {
  99. message: "bar",
  100. element: {
  101. name: "foo"
  102. }
  103. },
  104. {
  105. name: "required",
  106. message: "necessary",
  107. element: {
  108. name: "required"
  109. }
  110. }
  111. ];
  112. ok( !v.valid(), "form is not valid after adding errors manually" );
  113. v.showErrors();
  114. equal( 0, container.find( ".error:not(input)" ).length, "There should be no error label in the container" );
  115. equal( 2, labelcontainer.find( ".error:not(input)" ).length, "There should be two error labels in the labelcontainer" );
  116. equal( 2, labelcontainer.find( "li" ).length, "There should be two error lis in the labelcontainer" );
  117. ok( container.is( ":visible" ), "Check that the container is visible" );
  118. ok( labelcontainer.is( ":visible" ), "Check that the labelcontainer is visible" );
  119. labelcontainer.find( ".error:not(input)" ).each(function() {
  120. ok( $( this ).is( ":visible" ), "Check that each label is visible1" );
  121. equal( "li", $( this ).parent()[0].tagName.toLowerCase(), "Check that each label is wrapped in an li" );
  122. ok( $( this ).parent( "li" ).is( ":visible" ), "Check that each parent li is visible" );
  123. });
  124. });
  125. test( "errorcontainer, show/hide only on submit", function() {
  126. expect( 14 );
  127. var container = $( "#container" ),
  128. labelContainer = $( "#labelcontainer" ),
  129. v = $( "#testForm1" ).bind( "invalid-form.validate", function() {
  130. ok( true, "invalid-form event triggered called" );
  131. }).validate({
  132. errorContainer: container,
  133. errorLabelContainer: labelContainer,
  134. showErrors: function() {
  135. container.html( jQuery.validator.format( "There are {0} errors in your form.", this.numberOfInvalids()) );
  136. ok( true, "showErrors called" );
  137. this.defaultShowErrors();
  138. }
  139. });
  140. equal( "", container.html(), "must be empty" );
  141. equal( "", labelContainer.html(), "must be empty" );
  142. // validate whole form, both showErrors and invalidHandler must be called once
  143. // preferably invalidHandler first, showErrors second
  144. ok( !v.form(), "invalid form" );
  145. equal( 2, labelContainer.find( ".error:not(input)" ).length );
  146. equal( "There are 2 errors in your form.", container.html() );
  147. ok( labelContainer.is( ":visible" ), "must be visible" );
  148. ok( container.is( ":visible" ), "must be visible" );
  149. $( "#firstname" ).val( "hix" ).keyup();
  150. $( "#testForm1" ).triggerHandler( "keyup", [
  151. jQuery.event.fix({
  152. type: "keyup",
  153. target: $( "#firstname" )[ 0 ]
  154. })
  155. ]);
  156. equal( 1, labelContainer.find( ".error:visible" ).length );
  157. equal( "There are 1 errors in your form.", container.html() );
  158. $( "#lastname" ).val( "abc" );
  159. ok( v.form(), "Form now valid, trigger showErrors but not invalid-form" );
  160. });
  161. test( "test label used as error container", function(assert) {
  162. expect( 8 );
  163. var form = $( "#testForm16" ),
  164. field = $( "#testForm16text" );
  165. form.validate({
  166. errorPlacement: function( error, element ) {
  167. // Append error within linked label
  168. $( "label[for='" + element.attr( "id" ) + "']" ).append( error );
  169. },
  170. errorElement: "span"
  171. });
  172. ok( !field.valid() );
  173. equal( "Field Label", field.next( "label" ).contents().first().text(), "container label isn't disrupted" );
  174. assert.hasError(field, "missing");
  175. ok( !field.attr( "aria-describedby" ), "field does not require aria-describedby attribute" );
  176. field.val( "foo" );
  177. ok( field.valid() );
  178. equal( "Field Label", field.next( "label" ).contents().first().text(), "container label isn't disrupted" );
  179. ok( !field.attr( "aria-describedby" ), "field does not require aria-describedby attribute" );
  180. assert.noErrorFor(field);
  181. });
  182. test( "test error placed adjacent to descriptive label", function(assert) {
  183. expect( 8 );
  184. var form = $( "#testForm16" ),
  185. field = $( "#testForm16text" );
  186. form.validate({
  187. errorElement: "span"
  188. });
  189. ok( !field.valid() );
  190. equal( 1, form.find( "label" ).length );
  191. equal( "Field Label", form.find( "label" ).text(), "container label isn't disrupted" );
  192. assert.hasError( field, "missing" );
  193. field.val( "foo" );
  194. ok( field.valid() );
  195. equal( 1, form.find( "label" ).length );
  196. equal( "Field Label", form.find( "label" ).text(), "container label isn't disrupted" );
  197. assert.noErrorFor( field );
  198. });
  199. test( "test descriptive label used alongside error label", function(assert) {
  200. expect( 8 );
  201. var form = $( "#testForm16" ),
  202. field = $( "#testForm16text" );
  203. form.validate({
  204. errorElement: "label"
  205. });
  206. ok( !field.valid() );
  207. equal( 1, form.find( "label.title" ).length );
  208. equal( "Field Label", form.find( "label.title" ).text(), "container label isn't disrupted" );
  209. assert.hasError( field, "missing" );
  210. field.val( "foo" );
  211. ok( field.valid() );
  212. equal( 1, form.find( "label.title" ).length );
  213. equal( "Field Label", form.find( "label.title" ).text(), "container label isn't disrupted" );
  214. assert.noErrorFor( field );
  215. });
  216. test( "test custom errorElement", function(assert) {
  217. expect( 4 );
  218. var form = $( "#userForm" ),
  219. field = $( "#username" );
  220. form.validate({
  221. messages: {
  222. username: "missing"
  223. },
  224. errorElement: "label"
  225. });
  226. ok( !field.valid() );
  227. assert.hasError( field, "missing", "Field should have error 'missing'" );
  228. field.val( "foo" );
  229. ok( field.valid() );
  230. assert.noErrorFor( field, "Field should not have a visible error" );
  231. });
  232. test( "test existing label used as error element", function(assert) {
  233. expect( 4 );
  234. var form = $( "#testForm14" ),
  235. field = $( "#testForm14text" );
  236. form.validate({ errorElement: "label" });
  237. ok( !field.valid() );
  238. assert.hasError( field, "required" );
  239. field.val( "foo" );
  240. ok( field.valid() );
  241. assert.noErrorFor( field );
  242. });
  243. test( "test existing non-label used as error element", function(assert) {
  244. expect( 4 );
  245. var form = $( "#testForm15" ),
  246. field = $( "#testForm15text" );
  247. form.validate({ errorElement: "span" });
  248. ok( !field.valid() );
  249. assert.hasError( field, "required" );
  250. field.val( "foo" );
  251. ok( field.valid() );
  252. assert.noErrorFor( field );
  253. });
  254. test( "test existing non-error aria-describedby", function( assert ) {
  255. expect( 8 );
  256. var form = $( "#testForm17" ),
  257. field = $( "#testForm17text" );
  258. equal( field.attr( "aria-describedby" ), "testForm17text-description" );
  259. form.validate({ errorElement: "span" });
  260. ok( !field.valid() );
  261. equal( field.attr( "aria-describedby" ), "testForm17text-description testForm17text-error" );
  262. assert.hasError( field, "required" );
  263. field.val( "foo" );
  264. ok( field.valid() );
  265. assert.noErrorFor( field );
  266. strictEqual( "This is where you enter your data", $("#testForm17text-description").text() );
  267. strictEqual( "", $("#testForm17text-error").text(), "Error label is empty for valid field" );
  268. });