Chloro - Form Processing So Easy It Will Knock You Out
version 0.06
package MyApp::Form::Login; use Moose; use Chloro; field username => ( isa => 'Str', required => 1, ); field password => ( isa => 'Str', required => 1, ); ... sub login { ... my $form = MyApp::Form::Login->new(); my $resultset = $form->process( params => $submitted_params ); if ( $resultset->is_valid() ) { my $login = $resultset->results(); # Do something with $login->{username} & $login->{password} } else { # Errors that are not specific to just one field my @form_errors = $resultset->form_errors(); # Errors keyed by specific field names my %field_errors = $resultset->field_errors(); # Do something with these errors } }
This software is still very alpha, and the API may change without warning in future versions.
For a walkthrough of all this module's features, see Chloro::Manual.
Chloro is yet another in a long line of form processing libraries. It differs from other libraries in that it is entirely focused on defining forms in programmer terms. Field types are Moose type constraints, not HTML widgets ("Str" not "Select").
Chloro is focused on taking a browser's submission, doing basic validation, and returning a data structure that you can use for further processing.
Out of the box, it does not talk to your database, nor does it know anything about rendering HTML. However, it is designed so that these features could be provided by extensions.
Chloro starts with forms. A form is a class which uses Chloro (and Moose).
package MyApp::Form::User; use Moose; use Chloro; field username => ( isa => 'Str', required => 1, );
In order to validate data against a form, you instantiate a form object and call $form->process():
$form->process()
my $form = MyApp::Form::User->new(); my $resultset = $form->process( params => $params );
The $params are a hash reference where the keys are field names and the values are field values. Under the hood, you can define a variety of parameter munging and validation methods, or just use the defaults.
$params
The process() method returns a Chloro::ResultSet object. This object can tell you whether the submitted parameters were valid. If they weren't, you can dig into the errors associated with specific fields. You can also define validations against the form as a whole, and the resultset will have those errors too.
process()
if ( $resultset->is_valid() ) { ... } else { my $result = $resultset->result_for('username'); print $_->message() for $result->errors(); }
If the submission was valid, you can get the results as a hash reference:
my $hash = $resultset->results();
That's the basic workflow using Chloro.
If you're new to Chloro, you should start by reading Chloro::Manual.
Dave Rolsky <autarch@urth.org>
This software is Copyright (c) 2011 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install Chloro, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Chloro
CPAN shell
perl -MCPAN -e shell install Chloro
For more information on module installation, please visit the detailed CPAN module installation guide.