| 1 | package Yancy::Plugin::Form; | ||||||||||||
| 2 | 21 | our $VERSION = '1.088'; | |||||||||||
| 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<NOTE:> This API is B<EXPERIMENTAL> 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<Yancy::Plugin::Form::Bootstrap4> - Forms using L<Bootstrap 4|http://getbootstrap.com/docs/4.0/> | ||||||||||||
| 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<< <input> >>, C<< <textarea >>, | ||||||||||||
| 56 | #pod or C<< <select> >> element, but can be more. | ||||||||||||
| 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<Yancy::Guides::Schema/Types> 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<string> 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<Yancy::Guides::Schema/Types> 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<< <input> >> and C<< <textarea> >> elements. | ||||||||||||
| 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<Yancy::Guides::Schema/Declaring a Schema> 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<field_for>. | ||||||||||||
| 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</input_for>, 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<< <label> >>, the appropriate input (C<< <input> | ||||||||||||
| 195 | #pod >>, C<< <select> >>, or otherwise ), and any descriptive text. C<%args> | ||||||||||||
| 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<title> defined for this property | ||||||||||||
| 203 | #pod in the schema (see L<Yancy::Guides::Schema>), or the field's name. | ||||||||||||
| 204 | #pod | ||||||||||||
| 205 | #pod =item description | ||||||||||||
| 206 | #pod | ||||||||||||
| 207 | #pod The field's description. Optional. Defaults to the C<description> defined | ||||||||||||
| 208 | #pod for this property in the schema (see L<Yancy::Guides::Schema>). | ||||||||||||
| 209 | #pod | ||||||||||||
| 210 | #pod =item class | ||||||||||||
| 211 | #pod | ||||||||||||
| 212 | #pod A class to apply to the input element. See L</input>. | ||||||||||||
| 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<NOTE>: For CSRF tokens to work, this must be called with the current | ||||||||||||
| 226 | #pod controller, not with C<app>. 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<method> attribute for the C<< <form> >> tag. Defaults to C<POST>. | ||||||||||||
| 236 | #pod | ||||||||||||
| 237 | #pod =item action | ||||||||||||
| 238 | #pod | ||||||||||||
| 239 | #pod The C<action> URL for the C<< <form> >> tag. | ||||||||||||
| 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<item> in the stash (which is set by L<Yancy::Controller::Yancy/set>.) | ||||||||||||
| 245 | #pod | ||||||||||||
| 246 | #pod =item properties | ||||||||||||
| 247 | #pod | ||||||||||||
| 248 | #pod Arrayref of fields to show in this form. Defaults to the C<properties> | ||||||||||||
| 249 | #pod stash value (like the L<set action in Yancy::Controller::Yancy|Yancy::Controller::Yancy/set> 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<Yancy> | ||||||||||||
| 257 | #pod | ||||||||||||
| 258 | #pod =cut | ||||||||||||
| 259 | |||||||||||||
| 260 | 21 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 261 | 21 | ▶ | use Yancy::Util qw( currym ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 262 | |||||||||||||
| 263 | 53 | ▶ | sub register { | ||||||||||
|
Subroutine
register: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||
| 264 | 53 | my ( $self, $app, $conf ) = @_; | |||||||||||
| 265 | 53 | ▶ | my $prefix = $conf->{prefix} || 'form'; | ||||||||||
|
Condition50%1/2
$conf->{'prefix'} || 'form'
MC/DC0%0/1
$conf->{'prefix'} || 'form'
$conf->{'prefix'}
Truth table50%1/2
A
A= $conf->{'prefix'}
| |||||||||||||
| 266 | |||||||||||||
| 267 | 53 | $app->plugin( 'Mojolicious::Plugin::I18N' ); | |||||||||||
| 268 | |||||||||||||
| 269 | 53 | for my $method ( qw( form_for field_for input_for filter_for input ) ) { | |||||||||||
| 270 | 265 | $app->helper( "yancy.$prefix.$method" => currym( $self, $method ) ); | |||||||||||
| 271 | } | ||||||||||||
| 272 | } | ||||||||||||
| 273 | |||||||||||||
| 274 | 21 | 1; | |||||||||||
| 275 | |||||||||||||
| 276 | __END__ | ||||||||||||