| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Yancy::Plugin::Form; | 
| 2 |  |  |  |  |  |  | our $VERSION = '1.087'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: Generate form HTML using various UI libraries | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | #pod =head1 SYNOPSIS | 
| 6 |  |  |  |  |  |  | #pod | 
| 7 |  |  |  |  |  |  | #pod     use Mojolicious::Lite; | 
| 8 |  |  |  |  |  |  | #pod     plugin Yancy => { | 
| 9 |  |  |  |  |  |  | #pod         backend => 'pg://localhost/mysite', | 
| 10 |  |  |  |  |  |  | #pod         read_schema => 1, | 
| 11 |  |  |  |  |  |  | #pod     }; | 
| 12 |  |  |  |  |  |  | #pod     app->yancy->plugin( 'Form::Bootstrap4' ); | 
| 13 |  |  |  |  |  |  | #pod     app->routes->get( '/people/:id/edit' )->to( | 
| 14 |  |  |  |  |  |  | #pod         'yancy#set', | 
| 15 |  |  |  |  |  |  | #pod         schema => 'people', | 
| 16 |  |  |  |  |  |  | #pod         template => 'edit_people', | 
| 17 |  |  |  |  |  |  | #pod     ); | 
| 18 |  |  |  |  |  |  | #pod     app->start; | 
| 19 |  |  |  |  |  |  | #pod     __DATA__ | 
| 20 |  |  |  |  |  |  | #pod     @@ edit_people.html.ep | 
| 21 |  |  |  |  |  |  | #pod     %= $c->yancy->form->form_for( 'people' ); | 
| 22 |  |  |  |  |  |  | #pod | 
| 23 |  |  |  |  |  |  | #pod =head1 DESCRIPTION | 
| 24 |  |  |  |  |  |  | #pod | 
| 25 |  |  |  |  |  |  | #pod The Form plugins generate forms from JSON schemas. Plugin and | 
| 26 |  |  |  |  |  |  | #pod application developers can use the form plugin API to make forms, and | 
| 27 |  |  |  |  |  |  | #pod then sites can load a specific form library plugin to match the style of | 
| 28 |  |  |  |  |  |  | #pod the site. | 
| 29 |  |  |  |  |  |  | #pod | 
| 30 |  |  |  |  |  |  | #pod B This API is B and will be considered stable in | 
| 31 |  |  |  |  |  |  | #pod Yancy version 2.0. Please report any issues you have or features you'd | 
| 32 |  |  |  |  |  |  | #pod like to see. Minor things may change before version 2.0, so be sure to | 
| 33 |  |  |  |  |  |  | #pod read the release changelog before upgrading. | 
| 34 |  |  |  |  |  |  | #pod | 
| 35 |  |  |  |  |  |  | #pod =head2 Available Libraries | 
| 36 |  |  |  |  |  |  | #pod | 
| 37 |  |  |  |  |  |  | #pod =over | 
| 38 |  |  |  |  |  |  | #pod | 
| 39 |  |  |  |  |  |  | #pod =item * L - Forms using L | 
| 40 |  |  |  |  |  |  | #pod | 
| 41 |  |  |  |  |  |  | #pod =back | 
| 42 |  |  |  |  |  |  | #pod | 
| 43 |  |  |  |  |  |  | #pod =head1 HELPERS | 
| 44 |  |  |  |  |  |  | #pod | 
| 45 |  |  |  |  |  |  | #pod All form plugins add the same helpers with the same arguments so that | 
| 46 |  |  |  |  |  |  | #pod applications can use the form plugin that matches their site's | 
| 47 |  |  |  |  |  |  | #pod appearance. Yancy plugin and app developers should use form plugins to | 
| 48 |  |  |  |  |  |  | #pod build forms so that users can easily customize the form's appearance. | 
| 49 |  |  |  |  |  |  | #pod | 
| 50 |  |  |  |  |  |  | #pod =head2 yancy->form->input | 
| 51 |  |  |  |  |  |  | #pod | 
| 52 |  |  |  |  |  |  | #pod     my $html = $c->yancy->form->input( %args ); | 
| 53 |  |  |  |  |  |  | #pod     %= $c->yancy->form->plugin( %args ); | 
| 54 |  |  |  |  |  |  | #pod | 
| 55 |  |  |  |  |  |  | #pod Create a form input. Usually one of a C<<  >>, C<< | 
| 56 |  |  |  |  |  |  | #pod or C<< | 
| 57 |  |  |  |  |  |  | #pod | 
| 58 |  |  |  |  |  |  | #pod C<%args> is a list of name/value pairs with the following keys: | 
| 59 |  |  |  |  |  |  | #pod | 
| 60 |  |  |  |  |  |  | #pod =over | 
| 61 |  |  |  |  |  |  | #pod | 
| 62 |  |  |  |  |  |  | #pod =item type | 
| 63 |  |  |  |  |  |  | #pod | 
| 64 |  |  |  |  |  |  | #pod The type of the input field to create. One of the JSON schema types. | 
| 65 |  |  |  |  |  |  | #pod See L for details on the supported | 
| 66 |  |  |  |  |  |  | #pod types. | 
| 67 |  |  |  |  |  |  | #pod | 
| 68 |  |  |  |  |  |  | #pod =item name | 
| 69 |  |  |  |  |  |  | #pod | 
| 70 |  |  |  |  |  |  | #pod The name of the input. Required. | 
| 71 |  |  |  |  |  |  | #pod | 
| 72 |  |  |  |  |  |  | #pod =item value | 
| 73 |  |  |  |  |  |  | #pod | 
| 74 |  |  |  |  |  |  | #pod The value to show in the input. If not defined, will take the value from | 
| 75 |  |  |  |  |  |  | #pod the current request parameters. | 
| 76 |  |  |  |  |  |  | #pod | 
| 77 |  |  |  |  |  |  | #pod =item format | 
| 78 |  |  |  |  |  |  | #pod | 
| 79 |  |  |  |  |  |  | #pod For C types, the format the string should take. One of the | 
| 80 |  |  |  |  |  |  | #pod supported JSON schema formats, along with some additional ones. See | 
| 81 |  |  |  |  |  |  | #pod L for details on the supported | 
| 82 |  |  |  |  |  |  | #pod formats. | 
| 83 |  |  |  |  |  |  | #pod | 
| 84 |  |  |  |  |  |  | #pod =item pattern | 
| 85 |  |  |  |  |  |  | #pod | 
| 86 |  |  |  |  |  |  | #pod A regex pattern to validate the field before submit. | 
| 87 |  |  |  |  |  |  | #pod | 
| 88 |  |  |  |  |  |  | #pod =item required | 
| 89 |  |  |  |  |  |  | #pod | 
| 90 |  |  |  |  |  |  | #pod If true, the field will be marked as required. | 
| 91 |  |  |  |  |  |  | #pod | 
| 92 |  |  |  |  |  |  | #pod =item readonly | 
| 93 |  |  |  |  |  |  | #pod | 
| 94 |  |  |  |  |  |  | #pod If true, the field will be marked as read-only. | 
| 95 |  |  |  |  |  |  | #pod | 
| 96 |  |  |  |  |  |  | #pod =item disabled | 
| 97 |  |  |  |  |  |  | #pod | 
| 98 |  |  |  |  |  |  | #pod If true, the field will be marked as disabled. | 
| 99 |  |  |  |  |  |  | #pod | 
| 100 |  |  |  |  |  |  | #pod =item placeholder | 
| 101 |  |  |  |  |  |  | #pod | 
| 102 |  |  |  |  |  |  | #pod The placeholder for C<<  >> and C<< | 
| 103 |  |  |  |  |  |  | #pod | 
| 104 |  |  |  |  |  |  | #pod =item id | 
| 105 |  |  |  |  |  |  | #pod | 
| 106 |  |  |  |  |  |  | #pod The ID for this field. | 
| 107 |  |  |  |  |  |  | #pod | 
| 108 |  |  |  |  |  |  | #pod =item class | 
| 109 |  |  |  |  |  |  | #pod | 
| 110 |  |  |  |  |  |  | #pod A string with additional classes to add to this field. | 
| 111 |  |  |  |  |  |  | #pod | 
| 112 |  |  |  |  |  |  | #pod =item minlength | 
| 113 |  |  |  |  |  |  | #pod | 
| 114 |  |  |  |  |  |  | #pod The minimum length of the text in this field. Used to validate the form. | 
| 115 |  |  |  |  |  |  | #pod | 
| 116 |  |  |  |  |  |  | #pod =item maxlength | 
| 117 |  |  |  |  |  |  | #pod | 
| 118 |  |  |  |  |  |  | #pod The maximum length of the text in this field. Used to validate the form. | 
| 119 |  |  |  |  |  |  | #pod | 
| 120 |  |  |  |  |  |  | #pod =item minimum | 
| 121 |  |  |  |  |  |  | #pod | 
| 122 |  |  |  |  |  |  | #pod The minimum value for the number in this field. Used to validate the | 
| 123 |  |  |  |  |  |  | #pod form. | 
| 124 |  |  |  |  |  |  | #pod | 
| 125 |  |  |  |  |  |  | #pod =item maximum | 
| 126 |  |  |  |  |  |  | #pod | 
| 127 |  |  |  |  |  |  | #pod The maximum value for the number in this field. Used to validate the | 
| 128 |  |  |  |  |  |  | #pod form. | 
| 129 |  |  |  |  |  |  | #pod | 
| 130 |  |  |  |  |  |  | #pod =back | 
| 131 |  |  |  |  |  |  | #pod | 
| 132 |  |  |  |  |  |  | #pod Most of these properties are the same as the JSON schema field | 
| 133 |  |  |  |  |  |  | #pod properties. See L for details on | 
| 134 |  |  |  |  |  |  | #pod how Yancy translates JSON schema into forms. | 
| 135 |  |  |  |  |  |  | #pod | 
| 136 |  |  |  |  |  |  | #pod =head2 yancy->form->input_for | 
| 137 |  |  |  |  |  |  | #pod | 
| 138 |  |  |  |  |  |  | #pod     my $html = $c->yancy->form->input_for( $schema, $property, %args ); | 
| 139 |  |  |  |  |  |  | #pod     %= $c->yancy->form->input_for( $schema, $property, %args ); | 
| 140 |  |  |  |  |  |  | #pod | 
| 141 |  |  |  |  |  |  | #pod Create a form input for the given schema's property. This creates just | 
| 142 |  |  |  |  |  |  | #pod the input field, nothing else. To add a label, see C. | 
| 143 |  |  |  |  |  |  | #pod | 
| 144 |  |  |  |  |  |  | #pod C<%args> is a list of name/value pairs with the following keys: | 
| 145 |  |  |  |  |  |  | #pod | 
| 146 |  |  |  |  |  |  | #pod =over | 
| 147 |  |  |  |  |  |  | #pod | 
| 148 |  |  |  |  |  |  | #pod =item type | 
| 149 |  |  |  |  |  |  | #pod | 
| 150 |  |  |  |  |  |  | #pod =item value | 
| 151 |  |  |  |  |  |  | #pod | 
| 152 |  |  |  |  |  |  | #pod =item required | 
| 153 |  |  |  |  |  |  | #pod | 
| 154 |  |  |  |  |  |  | #pod =item format | 
| 155 |  |  |  |  |  |  | #pod | 
| 156 |  |  |  |  |  |  | #pod =item enum | 
| 157 |  |  |  |  |  |  | #pod | 
| 158 |  |  |  |  |  |  | #pod =item enum_labels | 
| 159 |  |  |  |  |  |  | #pod | 
| 160 |  |  |  |  |  |  | #pod =item class | 
| 161 |  |  |  |  |  |  | #pod | 
| 162 |  |  |  |  |  |  | #pod =back | 
| 163 |  |  |  |  |  |  | #pod | 
| 164 |  |  |  |  |  |  | #pod =head2 yancy->form->filter_for | 
| 165 |  |  |  |  |  |  | #pod | 
| 166 |  |  |  |  |  |  | #pod     my $html = $c->yancy->form->filter_for( $schema, $property, %args ); | 
| 167 |  |  |  |  |  |  | #pod     %= $c->yancy->form->filter_for( $schema, $property, %args ); | 
| 168 |  |  |  |  |  |  | #pod | 
| 169 |  |  |  |  |  |  | #pod Create a form input suitable as a filter for the given schema's | 
| 170 |  |  |  |  |  |  | #pod property. A filter input is never a required field, and always allows | 
| 171 |  |  |  |  |  |  | #pod some kind of "blank" value. The filter automatically captures a value | 
| 172 |  |  |  |  |  |  | #pod from the query parameter of the same name as the C<$property>. This | 
| 173 |  |  |  |  |  |  | #pod creates just the input field, nothing else. | 
| 174 |  |  |  |  |  |  | #pod | 
| 175 |  |  |  |  |  |  | #pod Takes the same C<%args> as L, with the following changes: | 
| 176 |  |  |  |  |  |  | #pod | 
| 177 |  |  |  |  |  |  | #pod =over | 
| 178 |  |  |  |  |  |  | #pod | 
| 179 |  |  |  |  |  |  | #pod =item * required is always false | 
| 180 |  |  |  |  |  |  | #pod | 
| 181 |  |  |  |  |  |  | #pod =item * format is always removed, to allow for partial searches | 
| 182 |  |  |  |  |  |  | #pod | 
| 183 |  |  |  |  |  |  | #pod =item * 'boolean' type fields become enum fields with 'yes', 'no', and | 
| 184 |  |  |  |  |  |  | #pod empty (either) options | 
| 185 |  |  |  |  |  |  | #pod | 
| 186 |  |  |  |  |  |  | #pod =back | 
| 187 |  |  |  |  |  |  | #pod | 
| 188 |  |  |  |  |  |  | #pod =head2 yancy->form->field_for | 
| 189 |  |  |  |  |  |  | #pod | 
| 190 |  |  |  |  |  |  | #pod     my $html = $c->yancy->form->field_for( $schema, $name, %args ); | 
| 191 |  |  |  |  |  |  | #pod     %= $c->yancy->form->field_for( $schema, $name, %args ); | 
| 192 |  |  |  |  |  |  | #pod | 
| 193 |  |  |  |  |  |  | #pod Generate a field for the given C<$schema> and property C<$name>. The | 
| 194 |  |  |  |  |  |  | #pod field will include a C<< | 
| 195 |  |  |  |  |  |  | #pod >>, C<< | 
| 196 |  |  |  |  |  |  | #pod is a hash with the following keys: | 
| 197 |  |  |  |  |  |  | #pod | 
| 198 |  |  |  |  |  |  | #pod =over | 
| 199 |  |  |  |  |  |  | #pod | 
| 200 |  |  |  |  |  |  | #pod =item title | 
| 201 |  |  |  |  |  |  | #pod | 
| 202 |  |  |  |  |  |  | #pod The field's title. Defaults to the C defined for this property | 
| 203 |  |  |  |  |  |  | #pod in the schema (see L), or the field's name. | 
| 204 |  |  |  |  |  |  | #pod | 
| 205 |  |  |  |  |  |  | #pod =item description | 
| 206 |  |  |  |  |  |  | #pod | 
| 207 |  |  |  |  |  |  | #pod The field's description. Optional. Defaults to the C defined | 
| 208 |  |  |  |  |  |  | #pod for this property in the schema (see L). | 
| 209 |  |  |  |  |  |  | #pod | 
| 210 |  |  |  |  |  |  | #pod =item class | 
| 211 |  |  |  |  |  |  | #pod | 
| 212 |  |  |  |  |  |  | #pod A class to apply to the input element. See L. | 
| 213 |  |  |  |  |  |  | #pod | 
| 214 |  |  |  |  |  |  | #pod =back | 
| 215 |  |  |  |  |  |  | #pod | 
| 216 |  |  |  |  |  |  | #pod =head2 yancy->form->form_for | 
| 217 |  |  |  |  |  |  | #pod | 
| 218 |  |  |  |  |  |  | #pod     my $html = $c->yancy->form->form_for( $schema, %args ); | 
| 219 |  |  |  |  |  |  | #pod     %= $c->yancy->form->plugin( $schema, %args ); | 
| 220 |  |  |  |  |  |  | #pod | 
| 221 |  |  |  |  |  |  | #pod Generate a form to edit an item from the given C<$schema>. The form | 
| 222 |  |  |  |  |  |  | #pod will include all the fields, a CSRF token, and a single button to submit | 
| 223 |  |  |  |  |  |  | #pod the form. | 
| 224 |  |  |  |  |  |  | #pod | 
| 225 |  |  |  |  |  |  | #pod B: For CSRF tokens to work, this must be called with the current | 
| 226 |  |  |  |  |  |  | #pod controller, not with C. To disable CSRF (not recommended), pass C<< | 
| 227 |  |  |  |  |  |  | #pod csrf => 0 >> in C<%args>. | 
| 228 |  |  |  |  |  |  | #pod | 
| 229 |  |  |  |  |  |  | #pod C<%args> is a list of name/value pairs with the following keys: | 
| 230 |  |  |  |  |  |  | #pod | 
| 231 |  |  |  |  |  |  | #pod =over | 
| 232 |  |  |  |  |  |  | #pod | 
| 233 |  |  |  |  |  |  | #pod =item method | 
| 234 |  |  |  |  |  |  | #pod | 
| 235 |  |  |  |  |  |  | #pod The C attribute for the C<< | 
| 236 |  |  |  |  |  |  | #pod | 
| 237 |  |  |  |  |  |  | #pod =item action | 
| 238 |  |  |  |  |  |  | #pod | 
| 239 |  |  |  |  |  |  | #pod The C URL for the C<< | 
| 240 |  |  |  |  |  |  | #pod | 
| 241 |  |  |  |  |  |  | #pod =item item | 
| 242 |  |  |  |  |  |  | #pod | 
| 243 |  |  |  |  |  |  | #pod A hashref of values to fill in the form. Defaults to the value of the | 
| 244 |  |  |  |  |  |  | #pod C  in the stash (which is set by L.)  | 
| 245 |  |  |  |  |  |  | #pod | 
| 246 |  |  |  |  |  |  | #pod =item properties | 
| 247 |  |  |  |  |  |  | #pod | 
| 248 |  |  |  |  |  |  | #pod Arrayref of fields to show in this form. Defaults to the C | 
| 249 |  |  |  |  |  |  | #pod stash value (like the L uses). | 
| 250 |  |  |  |  |  |  | #pod Otherwise, defaults to showing all fields except read-only fields. | 
| 251 |  |  |  |  |  |  | #pod | 
| 252 |  |  |  |  |  |  | #pod =back | 
| 253 |  |  |  |  |  |  | #pod | 
| 254 |  |  |  |  |  |  | #pod =head1 SEE ALSO | 
| 255 |  |  |  |  |  |  | #pod | 
| 256 |  |  |  |  |  |  | #pod L | 
| 257 |  |  |  |  |  |  | #pod | 
| 258 |  |  |  |  |  |  | #pod =cut | 
| 259 |  |  |  |  |  |  |  | 
| 260 | 19 |  |  | 19 |  | 15021 | use Mojo::Base 'Mojolicious::Plugin'; | 
|  | 19 |  |  |  |  | 48 |  | 
|  | 19 |  |  |  |  | 129 |  | 
| 261 | 19 |  |  | 19 |  | 3471 | use Yancy::Util qw( currym ); | 
|  | 19 |  |  |  |  | 63 |  | 
|  | 19 |  |  |  |  | 4518 |  | 
| 262 |  |  |  |  |  |  |  | 
| 263 |  |  |  |  |  |  | sub register { | 
| 264 | 52 |  |  | 52 | 1 | 166 | my ( $self, $app, $conf ) = @_; | 
| 265 | 52 |  | 50 |  |  | 450 | my $prefix = $conf->{prefix} || 'form'; | 
| 266 |  |  |  |  |  |  |  | 
| 267 | 52 |  |  |  |  | 301 | $app->plugin( 'Mojolicious::Plugin::I18N' ); | 
| 268 |  |  |  |  |  |  |  | 
| 269 | 52 |  |  |  |  | 30458 | for my $method ( qw( form_for field_for input_for filter_for input ) ) { | 
| 270 | 260 |  |  |  |  | 222665 | $app->helper( "yancy.$prefix.$method" => currym( $self, $method ) ); | 
| 271 |  |  |  |  |  |  | } | 
| 272 |  |  |  |  |  |  | } | 
| 273 |  |  |  |  |  |  |  | 
| 274 |  |  |  |  |  |  | 1; | 
| 275 |  |  |  |  |  |  |  | 
| 276 |  |  |  |  |  |  | __END__ |