-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
114 lines (87 loc) · 3.64 KB
/
README
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
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.