File Coverage

blib/lib/Class/MetaForm.pm
Criterion Covered Total %
statement 19 24 79.1
branch n/a
condition n/a
subroutine 6 7 85.7
pod 0 1 0.0
total 25 32 78.1


line stmt bran cond sub pod time code
1             package Class::MetaForm;
2              
3 3     3   1464258 use Moose ();
  3         500048  
  3         80  
4              
5 3     3   24 use Moose::Exporter;
  3         7  
  3         26  
6 3     3   163 use Moose::Util::MetaRole;
  3         9  
  3         85  
7 3     3   17 use Moose::Util::TypeConstraints qw/coerce from via/;
  3         5  
  3         30  
8              
9 3     3   4609 use namespace::autoclean;
  3         10215  
  3         18  
10              
11             our $VERSION = '0.01_02';
12              
13             Moose::Exporter->setup_import_methods;
14              
15             sub init_meta {
16 3     3 0 312 my ($self,%args) = @_;
17              
18 3         8 my $for_class = $args{ for_class };
19              
20 3         29 Moose->init_meta (%args);
21              
22 3         7174 $for_class->meta->error_class ('Class::MetaForm::Error');
23              
24 0           Moose::Util::MetaRole::apply_metaclass_roles (
25             for_class => $for_class,
26             metaclass_roles => [ 'Class::MetaForm::Meta::Class::Trait' ],
27             attribute_metaclass_roles => [ 'MooseX::MetaDescription::Meta::Trait' ],
28             );
29              
30 0           Moose::Util::MetaRole::apply_base_class_roles(
31             for_class => $for_class,
32             roles => [ 'Class::MetaForm::Filter' ],
33             );
34              
35             coerce $for_class
36             => from HashRef
37 0     0     => via { $for_class->new ($_) };
  0            
38            
39 0           return $for_class->meta;
40             }
41              
42             1;
43              
44             __END__
45              
46             =pod
47              
48             =head1 NAME
49              
50             Class::MetaForm - Gluing forms onto Moose
51              
52             =head1 WARNING
53              
54             This is a development version, treat it as such
55              
56             =head1 SYNOPSIS
57              
58             package My::ContactForm;
59              
60             use Moose;
61             use Class::MetaForm;
62              
63             has name => (
64             is => 'ro',
65             isa => 'Str',
66             required => 1,
67             );
68              
69             has age => (
70             is => 'ro',
71             isa => 'Int',
72             predicate => 'has_age',
73             );
74              
75             has email => (
76             is => 'ro',
77             isa => 'EmailType',
78             required => 1,
79             );
80              
81             # In a controller far, far away
82              
83             my $form = My::ContactForm->new ($c->req->params);
84              
85             # By the way, see Catalyst::Controller::MetaForm for more sugaring
86             # when using MetaForm with Catalyst.
87              
88             =head1 DESCRIPTION
89              
90             There are quite a few web form validation modules on CPAN. Some are
91             okay, most usually requires more effort using than what manual form
92             validation would require. For some time now, I've wanted to write a
93             form validator since I have yet to find one that I can really like.
94             As I just stated, most form validators requirse too much effort to use
95             and some want to run the entire show, trying to force your application
96             to be structured in a certain way. So my goals for a new validators
97             were to keep it simple and nonintrusive.
98              
99             So how does it actually work? L<Class::MetaForm> is in itself just
100             a sugar module for setting up some roles and traits in your class. It
101             really doesn't do a whole lot:
102              
103             =head2 Filtering
104              
105             Empty strings are removed from the arguments. This is because when a
106             field is present in a form, an empty string is used to indicate that
107             the field has not been filled in by the user, and hence would be
108             equivalent to not providing the parameter in Moose.
109              
110             It also converts an argument like:
111              
112             'foo.bar' => 42
113              
114             Into:
115              
116             foo => { bar => 42 }
117              
118             =head2 Attribute ordering
119              
120             A trait is added to the class in order to make attribute
121             initialization done in the same order that attributes were added to
122             the class.
123              
124             =head2 Exceptions
125              
126             Moose errors are difficult to understand; For a computer program
127             at least. Moose itself should be throwing real exception objects. But
128             it doesn't so we install some sugar that tries it best to parse the
129             error messages that Moose spits out.
130              
131             =head2 Hashref coercion
132              
133             So you can take adventage of the above hash filtering to have
134             multiple form objects as children of the current one.
135              
136             package My::AddressForm;
137              
138             use Moose;
139             use Class::MetaForm;
140              
141             has name => (
142             is => 'ro',
143             isa => 'Str',
144             );
145              
146             has zipcode => (
147             is => 'ro',
148             isa => 'Str',
149             );
150              
151             package My::RegisterForm;
152              
153             use Moose;
154             use Class::MetaForm;
155              
156             has billing => (
157             is => 'ro',
158             isa => 'My::AddressForm',
159             required => 1,
160             );
161              
162             Accepting the form parameters billing.name and billing.zipcode,
163             accessible as
164              
165             $form->billing->name
166              
167             =head2 MooseX::MetaDescription
168              
169             The trait provided by L<MooseX::MetaDescription> is added to your
170             attribute metaclass so you can provide additional descriptive
171             information in them. Most notably, the exception objects thrown will
172             use it to make the error message more human readable.
173              
174             has mysillyattributename => (
175             is => 'ro',
176             isa => 'Str',
177             description => { label => 'mylesssillyname' },
178             );
179              
180             =head1 SEE ALSO
181              
182             =over 4
183              
184             =item L<HTML::FormFu>
185              
186             =item L<HTML::FormHandler>
187              
188             =item L<Moose>
189              
190             =back
191              
192             =head1 BUGS
193              
194             Most software has bugs. This module probably isn't an exception.
195             If you find a bug please either email me, or add the bug to cpan-RT.
196              
197             =head1 AUTHOR
198              
199             Anders Nor Berle E<lt>berle@cpan.orgE<gt>
200              
201             =head1 COPYRIGHT AND LICENSE
202              
203             Copyright 2009 by Modula AS
204              
205             This library is free software; you can redistribute it and/or modify
206             it under the same terms as Perl itself.
207              
208             =cut
209