Class::MetaForm - Gluing forms onto Moose
This is a development version, treat it as such
package My::ContactForm; use Moose; use Class::MetaForm; has name => ( is => 'ro', isa => 'Str', required => 1, ); has age => ( is => 'ro', isa => 'Int', predicate => 'has_age', ); has email => ( is => 'ro', isa => 'EmailType', required => 1, ); # In a controller far, far away my $form = My::ContactForm->new ($c->req->params); # By the way, see Catalyst::Controller::MetaForm for more sugaring # when using MetaForm with Catalyst.
There are quite a few web form validation modules on CPAN. Some are okay, most usually requires more effort using than what manual form validation would require. For some time now, I've wanted to write a form validator since I have yet to find one that I can really like. As I just stated, most form validators requirse too much effort to use and some want to run the entire show, trying to force your application to be structured in a certain way. So my goals for a new validators were to keep it simple and nonintrusive.
So how does it actually work? Class::MetaForm is in itself just a sugar module for setting up some roles and traits in your class. It really doesn't do a whole lot:
Empty strings are removed from the arguments. This is because when a field is present in a form, an empty string is used to indicate that the field has not been filled in by the user, and hence would be equivalent to not providing the parameter in Moose.
It also converts an argument like:
'foo.bar' => 42
Into:
foo => { bar => 42 }
A trait is added to the class in order to make attribute initialization done in the same order that attributes were added to the class.
Moose errors are difficult to understand; For a computer program at least. Moose itself should be throwing real exception objects. But it doesn't so we install some sugar that tries it best to parse the error messages that Moose spits out.
So you can take adventage of the above hash filtering to have multiple form objects as children of the current one.
package My::AddressForm; use Moose; use Class::MetaForm; has name => ( is => 'ro', isa => 'Str', ); has zipcode => ( is => 'ro', isa => 'Str', ); package My::RegisterForm; use Moose; use Class::MetaForm; has billing => ( is => 'ro', isa => 'My::AddressForm', required => 1, );
Accepting the form parameters billing.name and billing.zipcode, accessible as
$form->billing->name
The trait provided by MooseX::MetaDescription is added to your attribute metaclass so you can provide additional descriptive information in them. Most notably, the exception objects thrown will use it to make the error message more human readable.
has mysillyattributename => ( is => 'ro', isa => 'Str', description => { label => 'mylesssillyname' }, );
Most software has bugs. This module probably isn't an exception. If you find a bug please either email me, or add the bug to cpan-RT.
Anders Nor Berle <berle@cpan.org>
Copyright 2009 by Modula AS
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Class::MetaForm, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::MetaForm
CPAN shell
perl -MCPAN -e shell install Class::MetaForm
For more information on module installation, please visit the detailed CPAN module installation guide.