| 1 |
|
| 2 |
|
| 3 |
NAME |
| 4 |
CGI::FormBuilder::Source::YAML - Initialize FormBuilder from YAML file |
| 5 |
|
| 6 |
SYNOPSIS |
| 7 |
use CGI::FormBuilder; |
| 8 |
|
| 9 |
my $form = CGI::FormBuilder->new( |
| 10 |
source => { |
| 11 |
source => 'form.fb', |
| 12 |
type => 'YAML', |
| 13 |
}, |
| 14 |
); |
| 15 |
|
| 16 |
my $lname = $form->field('lname'); # like normal |
| 17 |
|
| 18 |
DESCRIPTION |
| 19 |
This reads a YAML (YAML::Syck) file that contains FormBuilder config |
| 20 |
options and returns a hash to be fed to CGI::FormBuilder->new(). |
| 21 |
|
| 22 |
Instead of the syntax read by CGI::FormBuilder::Source::File, it uses |
| 23 |
YAML syntax as read by YAML::Syck. That means you fully specify the |
| 24 |
entire data structure. |
| 25 |
|
| 26 |
LoadCode is enabled, so you can use YAML syntax for defining subrou- |
| 27 |
tines. This is convenient if you have a function that generates vali- |
| 28 |
dation subrefs, for example, I have one that can check profanity using |
| 29 |
Regexp::Common. |
| 30 |
|
| 31 |
validate: |
| 32 |
myfield: |
| 33 |
javascript: /^[\s\S]{2,50}$/ |
| 34 |
perl: !!perl/code: >- |
| 35 |
{ My::Funk::fb_perl_validate({ |
| 36 |
min => 2, |
| 37 |
max => 50, |
| 38 |
profanity => 'check' |
| 39 |
})->(shift); |
| 40 |
} |
| 41 |
|
| 42 |
POST PROCESSING |
| 43 |
There are two exceptions to "pure YAML syntax" where this module does |
| 44 |
some post-processing of the result. |
| 45 |
|
| 46 |
REFERENCES (ala CGI::FormBuilder::Source::File) |
| 47 |
|
| 48 |
You can specify references as string values that start with \&, \$, \@, |
| 49 |
or \% in the same way you can with CGI::FormBuilder::Source::File. If |
| 50 |
you have a full direct package reference, it will look there, otherwise |
| 51 |
it will traverse up the caller stack and take the first it finds. |
| 52 |
|
| 53 |
For example, say your code serves multiple sites, and a menu gets dif- |
| 54 |
ferent options depending on the server name requested: |
| 55 |
|
| 56 |
# in My::Funk: |
| 57 |
our $food_options = { |
| 58 |
www.meats.com => [qw( beef chicken horta fish )], |
| 59 |
www.veggies.com => [qw( carrot apple quorn radish )], |
| 60 |
}; |
| 61 |
|
| 62 |
# in source file: |
| 63 |
options: \@{ $My::Funk::food_options->{ $ENV{SERVER_NAME} } } |
| 64 |
|
| 65 |
EVAL STRINGS |
| 66 |
|
| 67 |
You can specify an eval statement. You could achieve the same example |
| 68 |
a different way: |
| 69 |
|
| 70 |
options: eval { $My::Funk::food_options->{ $ENV{SERVER_NAME} }; } |
| 71 |
|
| 72 |
The cost either way is about the same -- the string is eval'd. |
| 73 |
|
| 74 |
EXAMPLE |
| 75 |
method: GET |
| 76 |
header: 0 |
| 77 |
title: test |
| 78 |
name: test |
| 79 |
action: /test |
| 80 |
submit: test it |
| 81 |
linebreaks: 1 |
| 82 |
|
| 83 |
required: |
| 84 |
- test1 |
| 85 |
- test2 |
| 86 |
|
| 87 |
fields: |
| 88 |
- test1 |
| 89 |
- test2 |
| 90 |
- test3 |
| 91 |
- test4 |
| 92 |
|
| 93 |
fieldopts: |
| 94 |
test1: |
| 95 |
type: text |
| 96 |
size: 10 |
| 97 |
maxlength: 32 |
| 98 |
|
| 99 |
test2: |
| 100 |
type: text |
| 101 |
size: 10 |
| 102 |
maxlength: 32 |
| 103 |
|
| 104 |
test3: |
| 105 |
type: radio |
| 106 |
options: |
| 107 |
- |
| 108 |
- 1 |
| 109 |
- Yes |
| 110 |
- |
| 111 |
- 0 |
| 112 |
- No |
| 113 |
|
| 114 |
test4: |
| 115 |
options: \@test4opts |
| 116 |
sort: \&Someother::Package::sortopts |
| 117 |
|
| 118 |
validate: |
| 119 |
test1: /^\w{3,10}$/ |
| 120 |
test2: |
| 121 |
javascript: EMAIL |
| 122 |
perl: eq '[email protected]' |
| 123 |
test3: |
| 124 |
- 0 |
| 125 |
- 1 |
| 126 |
test4: \@test4opts |
| 127 |
|
| 128 |
You get the idea. A bit more whitespace, but it works in a standard- |
| 129 |
ized way. |
| 130 |
|
| 131 |
METHODS |
| 132 |
new() |
| 133 |
|
| 134 |
Normally not used directly; it is called from CGI::FormBuilder. Cre- |
| 135 |
ates the "CGI::FormBuilder::Source::YAML" object. Arguments from the |
| 136 |
'source' hash passed to CGI::FormBuilder->new() will become defaults, |
| 137 |
unless specified in the file. |
| 138 |
|
| 139 |
parse($source) |
| 140 |
|
| 141 |
Normally not used directly; it is called from CGI::FormBuilder. Parses |
| 142 |
the specified source file. No fancy params -- just a single filename |
| 143 |
is accepted. If the file isn't acceptable to YAML::Syck, I suppose it |
| 144 |
will die. |
| 145 |
|
| 146 |
SEE ALSO |
| 147 |
CGI::FormBuilder, CGI::FormBuilder::Source |
| 148 |
|
| 149 |
AUTHOR |
| 150 |
Copyright (c) 2006 Mark Hedges <[email protected]>. All rights reserved. |
| 151 |
|
| 152 |
LICENSE |
| 153 |
This module is free software; you may copy it under terms of the Perl |
| 154 |
license (GNU General Public License or Artistic License.) |
| 155 |
http://www.opensource.org/licenses/index.html |
| 156 |
|