| 1 | package Dancer::Plugin::Form; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Dancer ':syntax'; | ||
| 7 | use Dancer::Plugin; | ||
| 8 | |||
| 9 | my %forms; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Dancer::Plugin::Form - Dancer form handler for Template::Flute template engine | ||
| 14 | |||
| 15 | =head1 VERSION | ||
| 16 | |||
| 17 | Version 0.0061 | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | our $VERSION = '0.0061'; | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | Display template with checkout form: | ||
| 26 | |||
| 27 | get '/checkout' => sub { | ||
| 28 | my $form; | ||
| 29 | |||
| 30 | $form = form('checkout'); | ||
| 31 | |||
| 32 | template 'checkout', {form => $form}; | ||
| 33 | }; | ||
| 34 | |||
| 35 | Retrieve form input from checkout form: | ||
| 36 | |||
| 37 | post '/checkout' => sub { | ||
| 38 | my ($form, $values); | ||
| 39 | |||
| 40 | $form = form('checkout'); | ||
| 41 | $values = $form->values(); | ||
| 42 | }; | ||
| 43 | |||
| 44 | Reset form after completion to prevent old data from | ||
| 45 | showing up on new form: | ||
| 46 | |||
| 47 | $form = form('checkout'); | ||
| 48 | $form->reset; | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | register form => sub { | ||
| 53 | my $name = ''; | ||
| 54 | my $object; | ||
| 55 | |||
| 56 | if (@_ % 2) { | ||
| 57 | $name = shift; | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | $name = 'main'; | ||
| 61 | } | ||
| 62 | |||
| 63 | $object = Dancer::Plugin::Form->new(name => $name, @_); | ||
| 64 | |||
| 65 | return $object; | ||
| 66 | }; | ||
| 67 | |||
| 68 | register_plugin; | ||
| 69 | |||
| 70 | =head1 DESCRIPTION | ||
| 71 | |||
| 72 | C<Dancer::Plugin::Form> is used for forms with the L<Dancer::Template::TemplateFlute> | ||
| 73 | templating engine. | ||
| 74 | |||
| 75 | Form fields, values and errors are stored into and loaded from the session key C<form>. | ||
| 76 | |||
| 77 | =head1 METHODS | ||
| 78 | |||
| 79 | =head2 new | ||
| 80 | |||
| 81 | Creates C<Dancer::Plugin::Form> object. | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub new { | ||
| 86 | my ($class, $self, %params); | ||
| 87 | |||
| 88 | $class = shift; | ||
| 89 | |||
| 90 | $self = {fields => [], errors => [], valid => undef, pristine => 1}; | ||
| 91 | bless $self; | ||
| 92 | |||
| 93 | %params = @_; | ||
| 94 | |||
| 95 | if (exists $params{name}) { | ||
| 96 | $self->{name} = $params{name}; | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | $self->{name} = 'main'; | ||
| 100 | } | ||
| 101 | |||
| 102 | if (exists $params{action}) { | ||
| 103 | $self->action($params{action}); | ||
| 104 | } | ||
| 105 | |||
| 106 | # try to load form data from session | ||
| 107 | $self->from_session(); | ||
| 108 | |||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | =head2 name | ||
| 113 | |||
| 114 | Get form name: | ||
| 115 | |||
| 116 | $form->name | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub name { | ||
| 121 | my $self = shift; | ||
| 122 | |||
| 123 | return $self->{name}; | ||
| 124 | } | ||
| 125 | |||
| 126 | =head2 action | ||
| 127 | |||
| 128 | Set form action: | ||
| 129 | |||
| 130 | $form->action('/checkout'); | ||
| 131 | |||
| 132 | Get form action: | ||
| 133 | |||
| 134 | $action = $form->action; | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub action { | ||
| 139 | my ($self, $action) = @_; | ||
| 140 | |||
| 141 | if ($action) { | ||
| 142 | $self->{action} = $action; | ||
| 143 | } | ||
| 144 | |||
| 145 | return $self->{action}; | ||
| 146 | } | ||
| 147 | |||
| 148 | =head2 fill | ||
| 149 | |||
| 150 | Fill form values: | ||
| 151 | |||
| 152 | $form->fill({username => 'racke', email => 'racke@linuxia.de'}); | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub fill { | ||
| 157 | my ($self); | ||
| 158 | |||
| 159 | $self = shift; | ||
| 160 | |||
| 161 | if (@_) { | ||
| 162 | if (@_ == 1) { | ||
| 163 | %{$self->{values}} = %{$_[0]}; | ||
| 164 | } | ||
| 165 | else { | ||
| 166 | %{$self->{values}} = @_; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | return $self->{values}; | ||
| 171 | } | ||
| 172 | |||
| 173 | =head2 values | ||
| 174 | |||
| 175 | Get form values as hash reference: | ||
| 176 | |||
| 177 | $values = $form->values; | ||
| 178 | |||
| 179 | Set form values from a hash reference: | ||
| 180 | |||
| 181 | $values => $form->values(ref => \%input); | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | sub values { | ||
| 186 | my ($self, $scope, $data) = @_; | ||
| 187 | my (%values, $params, $save); | ||
| 188 | |||
| 189 | |||
| 190 | if (! defined $scope) { | ||
| 191 | $params = params('body'); | ||
| 192 | $save = 1; | ||
| 193 | } | ||
| 194 | elsif ($scope eq 'session') { | ||
| 195 | $params = $self->{values}; | ||
| 196 | } | ||
| 197 | elsif ($scope eq 'body' || $scope eq 'query' ) { | ||
| 198 | $params = params($scope); | ||
| 199 | $save = 1; | ||
| 200 | } | ||
| 201 | elsif ($scope eq 'ref') { | ||
| 202 | $params = $data; | ||
| 203 | $save = 1; | ||
| 204 | } | ||
| 205 | else { | ||
| 206 | $params = ''; | ||
| 207 | } | ||
| 208 | |||
| 209 | for my $f (@{$self->{fields}}) { | ||
| 210 | $values{$f} = $params->{$f}; | ||
| 211 | |||
| 212 | if ($save && defined $values{$f}) { | ||
| 213 | # tidy form input first | ||
| 214 | $values{$f} =~ s/^\s+//; | ||
| 215 | $values{$f} =~ s/\s+$//; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | if ($save) { | ||
| 220 | $self->{values} = \%values; | ||
| 221 | return \%values; | ||
| 222 | } | ||
| 223 | |||
| 224 | return \%values; | ||
| 225 | } | ||
| 226 | |||
| 227 | =head2 valid | ||
| 228 | |||
| 229 | Determine whether form values are valid: | ||
| 230 | |||
| 231 | $form->valid(); | ||
| 232 | |||
| 233 | Return values are 1 (valid), 0 (invalid) or | ||
| 234 | undef (unknown). | ||
| 235 | |||
| 236 | Set form status to "valid": | ||
| 237 | |||
| 238 | $form->valid(1); | ||
| 239 | |||
| 240 | Set form status to "invalid": | ||
| 241 | |||
| 242 | $form->valid(0); | ||
| 243 | |||
| 244 | The form status automatically changes to | ||
| 245 | "invalid" when errors method is called with | ||
| 246 | error messages. | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | sub valid { | ||
| 251 | my $self = shift; | ||
| 252 | my $valid = shift; | ||
| 253 | |||
| 254 | if (defined $valid) { | ||
| 255 | Dancer::Logger::debug("Setting valid for $self->{name} to $valid."); | ||
| 256 | $self->{valid} = $valid; | ||
| 257 | |||
| 258 | # record changes in user's session | ||
| 259 | $self->to_session; | ||
| 260 | } | ||
| 261 | |||
| 262 | return $self->{valid}; | ||
| 263 | } | ||
| 264 | |||
| 265 | =head2 errors | ||
| 266 | |||
| 267 | Set form errors: | ||
| 268 | |||
| 269 | $form->errors({username => 'Minimum 8 characters', | ||
| 270 | email => 'Invalid email address'}); | ||
| 271 | |||
| 272 | Get form errors as hash reference: | ||
| 273 | |||
| 274 | $errors = $form->errors; | ||
| 275 | |||
| 276 | =cut | ||
| 277 | |||
| 278 | sub errors { | ||
| 279 | my ($self, $errors) = @_; | ||
| 280 | my ($key, $value, @buf); | ||
| 281 | |||
| 282 | if ($errors) { | ||
| 283 | if (ref($errors) eq 'HASH') { | ||
| 284 | while (($key, $value) = each %$errors) { | ||
| 285 | push @buf, {name => $key, label => $value}; | ||
| 286 | } | ||
| 287 | $self->{errors} = \@buf; | ||
| 288 | } | ||
| 289 | |||
| 290 | $self->{valid} = 0; | ||
| 291 | } | ||
| 292 | |||
| 293 | return $self->{errors}; | ||
| 294 | } | ||
| 295 | |||
| 296 | =head2 errors_hashed | ||
| 297 | |||
| 298 | Returns form errors as array reference filled with a hash reference | ||
| 299 | for each error. | ||
| 300 | |||
| 301 | =cut | ||
| 302 | |||
| 303 | sub errors_hashed { | ||
| 304 | my ($self) = @_; | ||
| 305 | my (@hashed); | ||
| 306 | |||
| 307 | for my $err (@{$self->{errors}}) { | ||
| 308 | push (@hashed, {name => $err->[0], label => $err->[1]}); | ||
| 309 | } | ||
| 310 | |||
| 311 | return \@hashed; | ||
| 312 | } | ||
| 313 | |||
| 314 | =head2 failure | ||
| 315 | |||
| 316 | Indicates form failure by passing form errors. | ||
| 317 | |||
| 318 | $form->failure(errors => {username => 'Minimum 8 characters', | ||
| 319 | email => 'Invalid email address'}); | ||
| 320 | |||
| 321 | You can also set a route for redirection: | ||
| 322 | |||
| 323 | return $form->failure(errors => {username => 'Minimum 8 characters'}, | ||
| 324 | route => '/account'); | ||
| 325 | |||
| 326 | Passing parameters for the redirection URL is also possible: | ||
| 327 | |||
| 328 | return $form->failure(errors => {username => 'Minimum 8 characters'}, | ||
| 329 | route => '/account', | ||
| 330 | params => {layout => 'mobile'}); | ||
| 331 | |||
| 332 | Please ensure that you validate input submitted by an user before | ||
| 333 | adding them to the C<params> hash. | ||
| 334 | |||
| 335 | =cut | ||
| 336 | |||
| 337 | sub failure { | ||
| 338 | my ($self, %args) = @_; | ||
| 339 | |||
| 340 | $self->{errors} = $args{errors}; | ||
| 341 | |||
| 342 | # update session data about this form | ||
| 343 | $self->to_session(); | ||
| 344 | |||
| 345 | session(form_errors => '<ul>' . join('', map {"<li>$_->[1]</li>"} @{$args{errors} || []}) . '</ul>'); | ||
| 346 | |||
| 347 | session(form_data => $args{data}); | ||
| 348 | |||
| 349 | if ($args{route}) { | ||
| 350 | redirect uri_for($args{route}, $args{params}); | ||
| 351 | } | ||
| 352 | |||
| 353 | return; | ||
| 354 | } | ||
| 355 | |||
| 356 | =head2 fields | ||
| 357 | |||
| 358 | Set form fields: | ||
| 359 | |||
| 360 | $form->fields([qw/username email password verify/]); | ||
| 361 | |||
| 362 | Get form fields: | ||
| 363 | |||
| 364 | $fields = $form->fields; | ||
| 365 | |||
| 366 | =cut | ||
| 367 | |||
| 368 | sub fields { | ||
| 369 | my ($self); | ||
| 370 | |||
| 371 | $self = shift; | ||
| 372 | |||
| 373 | if (@_) { | ||
| 374 | $self->{fields} = shift; | ||
| 375 | } | ||
| 376 | |||
| 377 | return $self->{fields}; | ||
| 378 | } | ||
| 379 | |||
| 380 | =head2 pristine | ||
| 381 | |||
| 382 | Determines whether a form is pristine or not. | ||
| 383 | |||
| 384 | This can be used to fill the form with default | ||
| 385 | values and suppress display of errors. | ||
| 386 | |||
| 387 | A form is pristine until it receives form | ||
| 388 | field input from the request or out of the | ||
| 389 | session. | ||
| 390 | |||
| 391 | =cut | ||
| 392 | |||
| 393 | sub pristine { | ||
| 394 | return $_[0]->{pristine}; | ||
| 395 | }; | ||
| 396 | |||
| 397 | =head2 reset | ||
| 398 | |||
| 399 | Reset form information (fields, errors, values, valid) and | ||
| 400 | updates session accordingly. | ||
| 401 | |||
| 402 | =cut | ||
| 403 | |||
| 404 | sub reset { | ||
| 405 | my $self = shift; | ||
| 406 | |||
| 407 | $self->{fields} = []; | ||
| 408 | $self->{errors} = []; | ||
| 409 | $self->{values} = {}; | ||
| 410 | $self->{valid} = undef; | ||
| 411 | $self->{pristine} = 1; | ||
| 412 | $self->to_session; | ||
| 413 | |||
| 414 | return 1; | ||
| 415 | } | ||
| 416 | |||
| 417 | =head2 from_session | ||
| 418 | |||
| 419 | Loads form data from session key 'form'. | ||
| 420 | Returns 1 if session contains data for this form, 0 otherwise. | ||
| 421 | |||
| 422 | =cut | ||
| 423 | |||
| 424 | sub from_session { | ||
| 425 | my ($self) = @_; | ||
| 426 | my ($forms_ref, $form); | ||
| 427 | |||
| 428 | if ($forms_ref = session('form')) { | ||
| 429 | if (exists $forms_ref->{$self->{name}}) { | ||
| 430 | $form = $forms_ref->{$self->{name}}; | ||
| 431 | |||
| 432 | $self->{fields} = $form->{fields} || []; | ||
| 433 | $self->{errors} = $form->{errors} || []; | ||
| 434 | $self->{values} = $form->{values} || {}; | ||
| 435 | $self->{valid} = $form->{valid}; | ||
| 436 | |||
| 437 | while (my ($key, $value) = each %{$self->{values}}) { | ||
| 438 | if (defined $value) { | ||
| 439 | $self->{pristine} = 0; | ||
| 440 | last; | ||
| 441 | } | ||
| 442 | } | ||
| 443 | |||
| 444 | return 1; | ||
| 445 | } | ||
| 446 | } | ||
| 447 | |||
| 448 | return 0; | ||
| 449 | } | ||
| 450 | |||
| 451 | =head2 to_session | ||
| 452 | |||
| 453 | Saves form name, form fields, form values and form errors into | ||
| 454 | session key 'form'. | ||
| 455 | |||
| 456 | =cut | ||
| 457 | |||
| 458 | sub to_session { | ||
| 459 | my ($self) = @_; | ||
| 460 | my ($forms_ref); | ||
| 461 | |||
| 462 | # get current form information from session | ||
| 463 | $forms_ref = session 'form'; | ||
| 464 | |||
| 465 | # update our form | ||
| 466 | $forms_ref->{$self->{name}} = {name => $self->{name}, | ||
| 467 | fields => $self->{fields}, | ||
| 468 | errors => $self->{errors}, | ||
| 469 | values => $self->{values}, | ||
| 470 | valid => $self->{valid}, | ||
| 471 | }; | ||
| 472 | |||
| 473 | # update form information | ||
| 474 | session 'form' => $forms_ref; | ||
| 475 | } | ||
| 476 | |||
| 477 | =head1 AUTHOR | ||
| 478 | |||
| 479 | Stefan Hornburg (Racke), C<< <racke at linuxia.de> >> | ||
| 480 | |||
| 481 | =head1 BUGS | ||
| 482 | |||
| 483 | Please report any bugs or feature requests to C<bug-dancer-template-templateflute at rt.cpan.org>, or through | ||
| 484 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer-Template-TemplateFlute>. I will be notified, and then you'll | ||
| 485 | automatically be notified of progress on your bug as I make changes. | ||
| 486 | |||
| 487 | |||
| 488 | |||
| 489 | |||
| 490 | =head1 SUPPORT | ||
| 491 | |||
| 492 | You can find documentation for this module with the perldoc command. | ||
| 493 | |||
| 494 | perldoc Dancer::Plugin::Form | ||
| 495 | |||
| 496 | |||
| 497 | You can also look for information at: | ||
| 498 | |||
| 499 | =over 4 | ||
| 500 | |||
| 501 | =item * RT: CPAN's request tracker | ||
| 502 | |||
| 503 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Template-TemplateFlute> | ||
| 504 | |||
| 505 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 506 | |||
| 507 | L<http://annocpan.org/dist/Dancer-Template-TemplateFlute> | ||
| 508 | |||
| 509 | =item * CPAN Ratings | ||
| 510 | |||
| 511 | L<http://cpanratings.perl.org/d/Dancer-Template-TemplateFlute> | ||
| 512 | |||
| 513 | =item * Search CPAN | ||
| 514 | |||
| 515 | L<http://search.cpan.org/dist/Dancer-Template-TemplateFlute/> | ||
| 516 | |||
| 517 | =back | ||
| 518 | |||
| 519 | |||
| 520 | =head1 ACKNOWLEDGEMENTS | ||
| 521 | |||
| 522 | |||
| 523 | =head1 LICENSE AND COPYRIGHT | ||
| 524 | |||
| 525 | Copyright 2011-2014 Stefan Hornburg (Racke). | ||
| 526 | |||
| 527 | This program is free software; you can redistribute it and/or modify it | ||
| 528 | under the terms of either: the GNU General Public License as published | ||
| 529 | by the Free Software Foundation; or the Artistic License. | ||
| 530 | |||
| 531 | See http://dev.perl.org/licenses/ for more information. | ||
| 532 | |||
| 533 | |||
| 534 | =cut | ||
| 535 | |||
| 536 | 1; # End of Dancer::Plugin::Form |