« blib/lib/CatalystX/Resource/Controller/Resource.pm
↑ Summary
lib/CatalystX/Resource/Controller/Resource.pm »
| 1 | package CatalystX::Resource::TraitFor::Controller::Resource::Form; | ||
| 2 | $CatalystX::Resource::TraitFor::Controller::Resource::Form::VERSION = '0.009_002'; | ||
| 3 | use MooseX::MethodAttributes::Role; | ||
| 4 | use MooseX::Types::LoadableClass qw/ LoadableClass /; | ||
| 5 | use namespace::autoclean; | ||
| 6 | |||
| 7 | # ABSTRACT: handles form related stuff | ||
| 8 | |||
| 9 | requires qw/ | ||
| 10 | resource_key | ||
| 11 | _msg | ||
| 12 | _redirect | ||
| 13 | /; | ||
| 14 | |||
| 15 | |||
| 16 | has 'form_class' => ( | ||
| 17 | is => 'rw', | ||
| 18 | required => 1, | ||
| 19 | isa => LoadableClass, | ||
| 20 | coerce => 1, | ||
| 21 | ); | ||
| 22 | |||
| 23 | |||
| 24 | has 'form_template' => ( | ||
| 25 | is => 'ro', | ||
| 26 | predicate => 'has_form_template', | ||
| 27 | ); | ||
| 28 | |||
| 29 | |||
| 30 | sub form { | ||
| 31 | my ( $self, $c, $activate_fields ) = @_; | ||
| 32 | |||
| 33 | my $resource = $c->stash->{ $self->resource_key }; | ||
| 34 | |||
| 35 | # activate_form_fields in stash overrides activate_fields from config | ||
| 36 | my $activate_form_fields = | ||
| 37 | exists $c->stash->{activate_form_fields} | ||
| 38 | ? $c->stash->{activate_form_fields} | ||
| 39 | : [@$activate_fields]; | ||
| 40 | |||
| 41 | my $form_attrs_new = | ||
| 42 | exists $c->stash->{form_attrs_new} ? $c->stash->{form_attrs_new} : {}; | ||
| 43 | my $form_attrs_process = | ||
| 44 | exists $c->stash->{form_attrs_process} ? $c->stash->{form_attrs_process} : {}; | ||
| 45 | |||
| 46 | my $form = $self->form_class->new(%$form_attrs_new); | ||
| 47 | $form->process( | ||
| 48 | active => $activate_form_fields, | ||
| 49 | item => $resource, | ||
| 50 | params => $c->req->params, | ||
| 51 | posted => ($c->req->method eq 'POST'), | ||
| 52 | %$form_attrs_process, | ||
| 53 | ); | ||
| 54 | |||
| 55 | if ( $self->has_form_template ) { | ||
| 56 | $c->stash( template => $self->form_template, form => $form ); | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | my $rendered_form = $form->render; | ||
| 60 | $c->stash( template => \$rendered_form ); | ||
| 61 | } | ||
| 62 | |||
| 63 | return unless $form->validated; | ||
| 64 | |||
| 65 | if ( $c->stash->{set_create_msg} ) { | ||
| 66 | $c->flash( msg => $self->_msg( $c, 'create' ) ); | ||
| 67 | } | ||
| 68 | elsif ( $c->stash->{set_update_msg} ) { | ||
| 69 | $c->flash( msg => $self->_msg( $c, 'update' ) ); | ||
| 70 | } | ||
| 71 | |||
| 72 | $self->_redirect($c); | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | 1; | ||
| 78 | |||
| 79 | __END__ | ||
| 80 | |||
| 81 | =pod | ||
| 82 | |||
| 83 | =encoding UTF-8 | ||
| 84 | |||
| 85 | =head1 NAME | ||
| 86 | |||
| 87 | CatalystX::Resource::TraitFor::Controller::Resource::Form - handles form related stuff | ||
| 88 | |||
| 89 | =head1 VERSION | ||
| 90 | |||
| 91 | version 0.009_002 | ||
| 92 | |||
| 93 | =head1 ATTRIBUTES | ||
| 94 | |||
| 95 | =head2 form_class | ||
| 96 | |||
| 97 | HTML::FormHandler class to use for this resource. | ||
| 98 | e.g.: 'MyApp::Form::Resources' | ||
| 99 | |||
| 100 | =head2 form_template | ||
| 101 | |||
| 102 | template file for HTML::FormHandler | ||
| 103 | optional, if you don't supply a form_template a stringified version will be used | ||
| 104 | |||
| 105 | =head1 METHODS | ||
| 106 | |||
| 107 | =head2 form | ||
| 108 | |||
| 109 | handle form validation, configuration of visible fields | ||
| 110 | and setting of notification messages | ||
| 111 | |||
| 112 | =head1 Catalyst stash variables | ||
| 113 | |||
| 114 | =head2 form_attrs_new | ||
| 115 | |||
| 116 | you can pass attributes to $form->new via $c->stash->{form_attrs_new} | ||
| 117 | |||
| 118 | $c->stash->{form_attrs_new} = { #... }; | ||
| 119 | |||
| 120 | =head2 form_attrs_process | ||
| 121 | |||
| 122 | you can pass attributes to $form->process via $c->stash->{form_attrs_process} | ||
| 123 | |||
| 124 | $c->stash->{form_attrs_process} = { posted => 0 }; | ||
| 125 | |||
| 126 | =head1 AUTHOR | ||
| 127 | |||
| 128 | David Schmidt <davewood@cpan.org> | ||
| 129 | |||
| 130 | =head1 COPYRIGHT AND LICENSE | ||
| 131 | |||
| 132 | This software is copyright (c) 2011 by David Schmidt. | ||
| 133 | |||
| 134 | This is free software; you can redistribute it and/or modify it under | ||
| 135 | the same terms as the Perl 5 programming language system itself. | ||
| 136 | |||
| 137 | =cut |