Skip to content

Commit 9048646

Browse files
scottwaltersampli
authored andcommitted
FormField macro for throwing Form controls directly into templates;
useful for using alternative or improved Form controls without having to change Asset source code.
1 parent 84c118a commit 9048646

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

docs/changelog/7.x.x.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
(Amir Plivatsky)
55
- fixed #12179: DataTable Field types get reset to "Text" in Edit Schema
66
(Amir Plivatsky)
7+
- added: FormField macro for rendering Form objects directly from templates
78

89
7.10.19
910
- fixed #12169: extras uploads symlink export

etc/WebGUI.conf.original

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@
832832
"FetchMimeType" : "FetchMimeType",
833833
"FilePump" : "FilePump",
834834
"FileUrl" : "FileUrl",
835+
"FormField" : "FormField",
835836
"GroupAdd" : "GroupAdd",
836837
"GroupDelete" : "GroupDelete",
837838
"GroupText" : "GroupText",

lib/WebGUI/Macro/FormField.pm

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package WebGUI::Macro::FormField;
2+
3+
#-------------------------------------------------------------------
4+
# WebGUI is Copyright 2001-2008 Plain Black Corporation.
5+
#-------------------------------------------------------------------
6+
# Please read the legal notices (docs/legal.txt) and the license
7+
# (docs/license.txt) that came with this distribution before using
8+
# this software.
9+
#-------------------------------------------------------------------
10+
# http://www.plainblack.com [email protected]
11+
#-------------------------------------------------------------------
12+
13+
use strict;
14+
15+
=head1 NAME
16+
17+
Package WebGUI::Macro::FormField
18+
19+
=head1 DESCRIPTION
20+
21+
Renders an instance of a Form object.
22+
23+
=head2 process( $session, $type, $field_name [, $default_value, @form_constructor_arguments ] )
24+
25+
C<$type> is one of the L<WebGUI::Form::Control> subclasses in L<WebGUI::Form::>.
26+
27+
C<$field_name> is the name the field will be given in the HTML "name" attribute.
28+
29+
C<$default_value> is the currently selected value to use for the form field if no GET/POST parameter or field of the
30+
current asset of the same name has a value.
31+
32+
A form posted form parameter of the same name as the C<$field_name>, if present, will be used instead of the default.
33+
Failing that, an attribute of the current asset of the same name, if present, will be used instead of the default value.
34+
35+
C<@form_constructor_arguments> get passed to the L<WebGUI::Form> subclass constructor.
36+
37+
=cut
38+
39+
40+
sub process {
41+
my $session = shift;
42+
my $type = shift;
43+
my $name = shift;
44+
my $default_value = shift || '';
45+
my @extras = @_;
46+
47+
my $form_class = "WebGUI::Form::" . ucfirst $type;
48+
49+
my $value = $session->form->get($name);
50+
$value ||= $session->asset->get($name) if $session->asset;
51+
$value ||= $default_value;
52+
53+
my $control = eval { WebGUI::Pluggable::instanciate($form_class, 'new', [ $session, { name => $name, value => $value, @extras } ]) };
54+
if ($@) {
55+
$session->log->warn("FormField Macro could not load class ``$form_class'': $@");
56+
return '';
57+
}
58+
59+
return $control->toHtml;
60+
}
61+
62+
1;
63+
64+

t/Macro/FormField.t

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#-------------------------------------------------------------------
2+
# WebGUI is Copyright 2001-2011 Plain Black Corporation.
3+
#-------------------------------------------------------------------
4+
# Please read the legal notices (docs/legal.txt) and the license
5+
# (docs/license.txt) that came with this distribution before using
6+
# this software.
7+
#-------------------------------------------------------------------
8+
# http://www.plainblack.com [email protected]
9+
#-------------------------------------------------------------------
10+
11+
use FindBin;
12+
use strict;
13+
use lib "$FindBin::Bin/../lib";
14+
15+
use WebGUI::Test;
16+
use WebGUI::Session;
17+
use WebGUI::Macro::AdminBar;
18+
use HTML::TokeParser;
19+
use HTML::Form;
20+
use Tie::IxHash;
21+
use WebGUI::Form_Checking;
22+
use WebGUI::Macro::FormField;
23+
24+
use Test::More; # increment this value for each test you create
25+
use Test::Deep;
26+
27+
use Data::Dumper;
28+
29+
my $session = WebGUI::Test->session;
30+
31+
# taken from t/Form/SelectList.t
32+
33+
my $testBlock = [
34+
{
35+
key => 'List1',
36+
testValue => [qw/a/],
37+
expected => 'a',
38+
comment => 'single element array, scalar',
39+
dataType => 'SCALAR'
40+
},
41+
{
42+
key => 'List2',
43+
testValue => [qw/a/],
44+
expected => 'EQUAL',
45+
comment => 'single element array, array',
46+
dataType => 'ARRAY'
47+
},
48+
{
49+
key => 'List3',
50+
testValue => [qw/a b c/],
51+
expected => "a\nb\nc",
52+
comment => 'multi element array, scalar',
53+
dataType => 'SCALAR'
54+
},
55+
{
56+
key => 'List4',
57+
testValue => [qw/a b c/],
58+
expected => 'EQUAL',
59+
comment => 'multi element array, array',
60+
dataType => 'ARRAY'
61+
},
62+
];
63+
64+
my $formType = 'SelectList';
65+
66+
my $output;
67+
$output = WebGUI::Macro::FormField::process(
68+
$session, 'SelectList', 'ListMultiple', [ qw(a c e), ''], # args to macro
69+
# args to particular Form subclass
70+
options => { a=>'aa', b=>'bb', c=>'cc', d=>'dd', e=>'ee', ''=>'Empty' },
71+
value => [ qw(a c e), ''],
72+
sortByValue => 1,
73+
);
74+
75+
warn $output;
76+
77+
my $numTests = 11 + scalar @{ $testBlock } + 1;
78+
79+
plan tests => $numTests;
80+
81+
my ($header, $footer) = (WebGUI::Form::formHeader($session), WebGUI::Form::formFooter($session));
82+
83+
my $html = join "\n", $header, $output, $footer;
84+
85+
my @forms = HTML::Form->parse($html, 'http://www.webgui.org');
86+
87+
##Test Form Generation
88+
89+
is(scalar @forms, 1, '1 form was parsed');
90+
91+
my $form = $forms[0];
92+
use Data::Dumper; warn Data::Dumper::Dumper $form; # XXX
93+
my @inputs = $form->inputs;
94+
is(scalar @inputs, 8, 'The form has 8 inputs');
95+
96+
#Basic tests
97+
98+
my @options = $form->find_input('ListMultiple');
99+
100+
is( scalar(grep {$_->type ne 'option'} @options), 0, 'All inputs are of type option');
101+
102+
is( scalar(grep {$_->{multiple} ne 'multiple'} @options), 0, 'All inputs have multiple');
103+
104+
my @names = map { $_->name } @options;
105+
cmp_deeply( [@names], bag(('ListMultiple')x6), 'correct number of names and names');
106+
107+
cmp_set([ $form->param('ListMultiple') ], [qw(a c e), ''], 'preselected values in order');
108+
109+
my @values = map { $_->possible_values } @options;
110+
cmp_bag([ @values ], [qw(a b c d e), '', (undef)x6], 'list of all options');
111+
112+
my @value_names = map { $_->value_names } @options;
113+
cmp_bag([ @value_names ], [qw(aa bb cc dd ee Empty), ('off')x6], 'list of all displayed value names');
114+

0 commit comments

Comments
 (0)