/t/render.t

http://github.com/gshank/html-formhandler · Raku · 250 lines · 212 code · 38 blank · 0 comment · 3 complexity · 1167828097a6d6f89a3accb298d6bbed MD5 · raw file

  1. use strict;
  2. use warnings;
  3. use Test::More;
  4. use HTML::FormHandler::Field::Text;
  5. {
  6. package Test::Form;
  7. use HTML::FormHandler::Moose;
  8. extends 'HTML::FormHandler';
  9. with 'HTML::FormHandler::Render::Simple';
  10. has '+name' => ( default => 'testform' );
  11. has_field 'test_field' => (
  12. input_class => 'test123',
  13. size => 20,
  14. label => 'TEST',
  15. id => 'f99',
  16. );
  17. has_field 'number';
  18. has_field 'fruit' => ( type => 'Select' );
  19. has_field 'cheese' => ( type => 'Select' );
  20. has_field 'vegetables' => ( type => 'Multiple' );
  21. has_field 'grains' => ( type => 'Multiple' );
  22. has_field 'opt_in' => ( type => 'Select', widget => 'radio_group',
  23. options => [{ value => 0, label => 'No'}, { value => 1, label => 'Yes'} ] );
  24. has_field 'starch' => ( type => 'Multiple', widget => 'checkbox_group',
  25. options => [{ value => 1, label => 'One'}, { value => 2, label => 'Two'},
  26. { value => 3, label => 'Three' },
  27. ] );
  28. has_field 'active' => ( type => 'Checkbox' );
  29. has_field 'comments' => ( type => 'TextArea', cols => 40, rows => 3 );
  30. has_field 'hidden' => ( type => 'Hidden' );
  31. has_field 'selected' => ( type => 'Boolean' );
  32. has_field 'start_date' => ( type => 'DateTime' );
  33. has_field 'start_date.month' => ( type => 'Integer', range_start => 1,
  34. range_end => 12 );
  35. has_field 'start_date.day' => ( type => 'Integer', range_start => 1,
  36. range_end => 31 );
  37. has_field 'start_date.year' => ( type => 'Integer', range_start => 2000,
  38. range_end => 2020 );
  39. has_field 'two_errors' => (
  40. apply => [
  41. { check => [ ], message => 'First constraint error' },
  42. { check => [ ], message => 'Second constraint error' }
  43. ]
  44. );
  45. has_field 'submit' => ( type => 'Submit', value => 'Update' );
  46. has '+dependency' => ( default => sub { [ ['start_date.month',
  47. 'start_date.day', 'start_date.year'] ] } );
  48. has_field 'no_render' => ( widget => 'no_render' );
  49. sub options_fruit {
  50. return (
  51. 1 => 'apples',
  52. 2 => 'oranges',
  53. 3 => 'kiwi',
  54. );
  55. }
  56. sub options_vegetables {
  57. return (
  58. 1 => 'lettuce',
  59. 2 => 'broccoli',
  60. 3 => 'carrots',
  61. 4 => 'peas',
  62. );
  63. }
  64. sub options_grains {
  65. return [
  66. { value => 1, label => 'maize', disabled => 0 },
  67. { value => 2, label => 'rice', disabled => 1 },
  68. { value => 3, label => 'wheat' },
  69. ];
  70. }
  71. sub options_cheese {
  72. return [
  73. { value => 1, label => 'canastra', disabled => 0 },
  74. { value => 2, label => 'brie', disabled => 1 },
  75. { value => 3, label => 'gorgonzola' },
  76. ];
  77. }
  78. }
  79. my $form = Test::Form->new;
  80. ok( $form, 'create form');
  81. my $params = {
  82. test_field => 'something',
  83. number => 0,
  84. fruit => 2,
  85. vegetables => [2,4],
  86. active => 'now',
  87. comments => 'Four score and seven years ago...',
  88. hidden => '1234',
  89. selected => '1',
  90. 'start_date.month' => '7',
  91. 'start_date.day' => '14',
  92. 'start_date.year' => '2006',
  93. two_errors => 'aaa',
  94. opt_in => 0,
  95. };
  96. $form->process( $params );
  97. is_deeply( $form->field('starch')->input_without_param, [], 'checkbox group settings' );
  98. is( $form->field('starch')->not_nullable, 1, 'checkbox group settings' );
  99. is_deeply( $form->value->{starch}, [], 'checkbox group value' );
  100. is( $form->render_field( $form->field('number') ),
  101. '
  102. <div><label class="label" for="number">Number: </label><input type="text" name="number" id="number" value="0" /></div>
  103. ',
  104. "value '0' is rendered"
  105. );
  106. my $output1 = $form->render_field( $form->field('test_field') );
  107. is( $output1,
  108. '
  109. <div><label class="label" for="f99">TEST: </label><input type="text" name="test_field" id="f99" size="20" value="something" class="test123" /></div>
  110. ',
  111. 'output from text field');
  112. my $output2 = $form->render_field( $form->field('fruit') );
  113. is( $output2,
  114. '
  115. <div><label class="label" for="fruit">Fruit: </label><select name="fruit" id="fruit"><option value="1" id="fruit.0">apples</option><option value="2" id="fruit.1" selected="selected">oranges</option><option value="3" id="fruit.2">kiwi</option></select></div>
  116. ',
  117. 'output from select field');
  118. my $output12 = $form->render_field( $form->field('cheese') );
  119. is( $output12,
  120. '
  121. <div><label class="label" for="cheese">Cheese: </label><select name="cheese" id="cheese"><option value="1" id="cheese.0">canastra</option><option value="2" id="cheese.1" disabled="disabled">brie</option><option value="3" id="cheese.2">gorgonzola</option></select></div>
  122. ',
  123. 'output from select field with disabled option');
  124. my $output3 = $form->render_field( $form->field('vegetables') );
  125. is( $output3,
  126. '
  127. <div><label class="label" for="vegetables">Vegetables: </label><select name="vegetables" id="vegetables" multiple="multiple" size="5"><option value="1" id="vegetables.0">lettuce</option><option value="2" id="vegetables.1" selected="selected">broccoli</option><option value="3" id="vegetables.2">carrots</option><option value="4" id="vegetables.3" selected="selected">peas</option></select></div>
  128. ',
  129. 'output from select multiple field');
  130. my $output13 = $form->render_field( $form->field('grains') );
  131. is( $output13,
  132. '
  133. <div><label class="label" for="grains">Grains: </label><select name="grains" id="grains" multiple="multiple" size="5"><option value="1" id="grains.0">maize</option><option value="2" id="grains.1" disabled="disabled">rice</option><option value="3" id="grains.2">wheat</option></select></div>
  134. ',
  135. 'output from select multiple field with disabled option');
  136. my $output4 = $form->render_field( $form->field('active') );
  137. is( $output4,
  138. '
  139. <div><label class="label" for="active">Active: </label><input type="checkbox" name="active" id="active" value="1" /></div>
  140. ',
  141. 'output from checkbox field');
  142. my $output5 = $form->render_field( $form->field('comments') );
  143. is( $output5,
  144. '
  145. <div><label class="label" for="comments">Comments: </label><textarea name="comments" id="comments" rows="3" cols="40">Four score and seven years ago...</textarea></div>
  146. ',
  147. 'output from textarea' );
  148. my $output6 = $form->render_field( $form->field('hidden') );
  149. is( $output6,
  150. '
  151. <div><input type="hidden" name="hidden" id="hidden" value="1234" /></div>
  152. ',
  153. 'output from hidden field' );
  154. my $output7 = $form->render_field( $form->field('selected') );
  155. is( $output7,
  156. '
  157. <div><label class="label" for="selected">Selected: </label><input type="checkbox" name="selected" id="selected" value="1" checked="checked" /></div>
  158. ',
  159. 'output from boolean' );
  160. my $output8 = $form->render_field( $form->field('start_date') );
  161. is( $output8,
  162. '
  163. <div><fieldset class="start_date"><legend>Start date</legend>
  164. <div><label class="label" for="start_date.month">Month: </label><input type="text" name="start_date.month" id="start_date.month" size="8" value="7" /></div>
  165. <div><label class="label" for="start_date.day">Day: </label><input type="text" name="start_date.day" id="start_date.day" size="8" value="14" /></div>
  166. <div><label class="label" for="start_date.year">Year: </label><input type="text" name="start_date.year" id="start_date.year" size="8" value="2006" /></div>
  167. </fieldset></div>
  168. ',
  169. 'output from DateTime' );
  170. my $output9 = $form->render_field( $form->field('submit') );
  171. is( $output9, '
  172. <div><input type="submit" name="submit" id="submit" value="Update" /></div>
  173. ', 'output from Submit');
  174. my $output10 = $form->render_field( $form->field('opt_in') );
  175. is( $output10, '
  176. <div><label class="label" for="opt_in">Opt in: </label> <br /><label for="opt_in.0"><input type="radio" value="0" name="opt_in" id="opt_in.0" checked="checked" />No</label><br /><label for="opt_in.1"><input type="radio" value="1" name="opt_in" id="opt_in.1" />Yes</label><br /></div>
  177. ', 'output from radio group' );
  178. my $output11 = $form->render_start;
  179. is( $output11,'<form id="testform" method="post" >
  180. <fieldset class="main_fieldset">', 'Form start OK' );
  181. my $output = $form->render;
  182. ok( $output, 'get rendered output from form');
  183. is( $form->render_field( $form->field('no_render')), '', 'no_render' );
  184. {
  185. package Test::Field::Rendering;
  186. use HTML::FormHandler::Moose;
  187. extends 'HTML::FormHandler';
  188. has_field 'my_html' => ( type => 'Display', html => '<h2>You got here!</h2>' );
  189. has_field 'explanation' => ( type => 'Display' );
  190. has_field 'between' => ( type => 'Display', set_html => 'between_html' );
  191. has_field 'nolabel' => ( type => 'Text', no_render_label => 1 );
  192. sub html_explanation {
  193. my ( $self, $field ) = @_;
  194. return "<p>I have an explanation somewhere around here...</p>";
  195. }
  196. sub between_html {
  197. my ( $self, $field ) = @_;
  198. return "<div>Somewhere, over the rainbow...</div>";
  199. }
  200. }
  201. $form = Test::Field::Rendering->new;
  202. is( $form->field('my_html')->render, '<h2>You got here!</h2>', 'display field renders' );
  203. is( $form->field('explanation')->render, '<p>I have an explanation somewhere around here...</p>',
  204. 'display field renders with form method' );
  205. is( $form->field('between')->render, '<div>Somewhere, over the rainbow...</div>',
  206. 'set_html field renders' );
  207. is( $form->field('nolabel')->render, '
  208. <div><input type="text" name="nolabel" id="nolabel" value="" /></div>
  209. ', 'no_render_label works');
  210. done_testing;