/usr/share/perl5/HTML/FormHandler/Manual/Catalyst.pod is in libhtml-formhandler-perl 0.40067-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | package HTML::FormHandler::Manual::Catalyst;
# ABSTRACT: using HFH forms in Catalyst
__END__
=pod
=encoding UTF-8
=head1 NAME
HTML::FormHandler::Manual::Catalyst - using HFH forms in Catalyst
=head1 VERSION
version 0.40067
=head1 SYNOPSIS
L<Manual Index|HTML::FormHandler::Manual>
This part of the FormHandler Manual describes the use of the L<HTML::FormHandler>
package in Catalyst controllers.
See the other FormHandler documentation at L<HTML::FormHandler::Manual>, or
the base class at L<HTML::FormHandler>.
=head1 DESCRIPTION
Although L<HTML::FormHandler> can be used in any Perl web application, module, or
script, one of its most common uses is in L<Catalyst> applications.
Using a form takes only a few lines of code, so it's not necessary to have
a L<Catalyst> base controller, although you could make a base controller for
FormHandler if you're doing more than the basics.
=head2 A Controller Example
The following example uses chained dispatching. The 'form' method is called
by both the create and edit actions.
package BookDB::Controller::Borrower;
use Moose;
BEGIN { extends 'Catalyst::Controller' }
use BookDB::Form::Borrower;
sub borrower_base : Chained PathPart('borrower') CaptureArgs(0) { }
sub list : Chained('borrower_base') PathPart('list') Args(0) {
my ( $self, $c ) = @_;
my $borrowers = [ $c->model('DB::Borrower')->all ];
my @columns = ( 'name', 'email' );
$c->stash( borrowers => $borrowers, columns => \@columns,
template => 'borrower/list.tt' );
}
sub add : Chained('borrower_base') PathPart('add') Args(0) {
my ( $self, $c ) = @_;
# Create the empty borrower row for the form
$c->stash( borrower => $c->model('DB::Borrower')->new_result({}) );
return $self->form($c);
}
sub item : Chained('borrower_base') PathPart('') CaptureArgs(1) {
my ( $self, $c, $borrower_id ) = @_;
$c->stash( borrower => $c->model('DB::Borrower')->find($borrower_id) );
}
sub edit : Chained('item') PathPart('edit') Args(0) {
my ( $self, $c ) = @_;
return $self->form($c);
}
sub form {
my ( $self, $c ) = @_;
my $form = BookDB::Form::Borrower->new;
$c->stash( form => $form, template => 'borrower/form.tt' );
return unless $form->process( item => $c->stash->{borrower},
params => $c->req->parameters );
$c->res->redirect( $c->uri_for($self->action_for('list')) );
}
sub delete : Chained('item') PathPart('delete') Args(0) {
my ( $self, $c ) = @_;
$c->stash->{borrower}->delete;
$c->res->redirect( $c->uri_for($c->action_for('list')) );
}
1;
=head2 Another way to set up your form
If you are setting the schema or other form attributes (such as the user_id,
or other attributes) on your form you could create a base controller that would set
these in the form on each call using L<Catalyst::Component::InstancePerContext>,
or set them in a base Chained method.
sub book_base : Chained PathPart('book') CaptureArgs(0) {
my ( $self, $c ) = @_;
my $form = MyApp::Form->new;
$form->schema( $c->model('DB')->schema );
$form->params( $c->req->parameters );
$form->user_id( $c->user->id );
$c->stash( form => $form );
}
Then you could just pass in the item_id when the form is processed.
return unless $c->stash->{form}->process( item_id => $id );
=head2 Putting a form in a Moose attribute
You can also put your form in a Moose attribute in the controller.
package MyApp::Controller::Book;
use Moose;
BEGIN { extends 'Catalyst::Controller'; }
use MyApp::Form::Book;
has 'edit_form' => ( isa => 'MyApp::Form::Book', is => 'rw',
lazy => 1, default => sub { MyApp::Form::Book->new } );
Then you can process the form in your actions with
C<< $self->edit_form->process( params => $c->req->body_parameters ); >> or
C<< my $result = $self->edit_form->run( params => $c->req->body_parameters ); >>.
=head2 Using HTML::FillInForm
If you want to use L<HTML::FillInForm> to fill in values instead of
doing it in directly in a template using either the field or the form 'fif'
methods, you can use L<Catalyst::View::FillInForm> on your view class:
package MyApp::View::TT;
use Moose;
with 'Catalyst::View::FillInForm';
....
1;
and set the 'fif' hash in the 'fillinform' stash variable:
$self->form->process( ... );
$c->stash( fillinform => $self->form->fif );
return unless $form->validated;
When the 'fillinform' stash variable is set, HTML::FillInForm will automatically
be used by your view to fill in the form values. This can be very helpful
when you want to build your forms by hand, or when you have legacy forms that
you're just trying to hook up to FormHandler.
=head2 The Catalyst context
FormHandler has a 'ctx' attribute that can be used to set the Catalyst context (or
anything you want, really). But if you can avoid passing in the context, you should do so,
because you're mixing up your MVC and it makes it much more difficult to test your
forms. But if you need to do it, you can:
my $form = MyApp::Form->new( ctx => $c );
Usually you should prefer to add new attributes to your form:
package MyApp::Form;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
has 'user_id' => ( is => 'rw' );
has 'hostname' => ( is => 'rw' );
has 'captcha_store' => ( is => 'rw' );
....
1;
Then just pass the attributes in on new:
my $form => MyApp::Form->new( user_id => $c->user->id, hostname => $c->req->host,
captcha_store => $c->{session}->{captcha} );
Or set them using accessors:
$form->user_id( $c->user->id );
$form->hostname( $c->req->host );
$form->captcha_store( $c->{session}->{captcha} );
Then you can access these attributes in your form validation methods:
sub validate_selection {
my ( $self, $field ) = @_;
if( $field->value eq 'something' && $self->hostname eq 'something_else' )
{
$field->add_error("some error message" );
}
}
=head1 AUTHOR
FormHandler Contributors - see HTML::FormHandler
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by Gerda Shank.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|