NAME
Spark Form - A simple yet powerful forms validation system that promotes reuse.
SYNOPSIS
use Spark::Form;
use CGI; #Because it makes for a quick and oversimplistic example
use Third::Party::Field;
$form = Spark::Form->new(plugin_ns => 'MyApp::Field');
# Add a couple of inbuilt modules
$form->add('email','email',confirm_field => 'email-confirm')
->add('email','email-confirm')
->add('password','password',regex => qr/^\S{6,}$/),
#This one will be autoloaded from MyApp::Field::Username
->add('username','username')
# And this shows how you can use a third party field of any class name
->add(Third::Party::Field->new(name => 'blah'));
#Pass in a hashref of params to populate the virtual form with data
$form->data(CGI->new->params);
#And do the actual validation
if ($form->validate) {
print "You are now registered";
} else {
print join "\n", $form->errors;
}
and over in MyApp/Field/Username.pm...
package MyApp::Form::Field::Username;
use base Spark::Form::Field;
sub validate {
my ($self,$v) = @_;
if (length $v < 6 or length $v > 12) {
$self->error("Usernames must be 6-12 characters long");
} elsif ($v =~ /[^a-zA-Z0-9_-]/) {
$self->error("Usernames may contain only a-z,A-Z,0-9, _ and -");
} else {
$self->error(undef);
}
$self->valid(!!$self->error());
}
DEPENDENCIES
Moose. I've dropped using Any::Moose. If you need the performance increase, perhaps it's time to start thinking about shifting off CGI.
METHODS
import (%options)
Allows you to set some options for the forms class.
- class => String
-
Optional, gives the basename for searching for form plugins.
Given 'MyApp', it will try to load form plugins from MyApp::Form::Field::*
- source => String
-
Optional, names a plugin to try and extract form data from.
If unspecified, you will need to call $form->data(\%data);
add ($thing,@rest)
If $thing is a string, attempts to instantiate a plugin of that type and add it to the form. Requires the second argument to be a string name for the field to identify it in the form. Rest will become %kwargs If it is an arrayref, it loops over the contents (Useful for custom fields, will probably result in bugs for string field names).@rest will be passed in each iteration. If it looks sufficiently like a field (implements Spark::Form::Field), then it will add it to the list of fields. @rest will just become %kwargs
Uses 'field name' to locate it from the data passed in.
This is a streaming interface, it returns the form itself.
validate
Validates the form. Sets valid
and then also returns the value.
data
Allows you to pass in a hashref of data to populate the fields with before validation. Useful if you don't use a plugin to automatically populate the data.
This is a streaming interface, it returns the form itself.
BUILD
Moose constructor. Test::Pod::Coverage made me do it. Adds class
to the search path for field modules.
get (Str)
Returns the form field of that name
Docs?
http://sparkengine.org/docs/forms/
Source?
http://github.com/jjl/Spark-Form/
AUTHOR
James Laver. http://jameslaver.com/.
Thanks to the Django Project, whose forms module gave some inspiration.
SEE ALSO
The FAQ: Spark::Form::FAQ
LICENSE
Copyright (C) 2009 James Laver
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.