[Catalyst] HTML::Widget forms
Maurice Height
mauriceh at bigpond.net.au
Thu Mar 2 05:10:15 CET 2006
I am taking my first baby steps with Catalyst and HTML::Widgets.
I have included some of my very simple code below (with line numbers)
The sub 'formA' code displays a form and continues to so while any errors
are generated by the constraints. I don't know if this is a reasonable way
to process forms with HTML::Widget but it seems to mostly work OK.
However...it is possible to turn off the generation of <span> error messages
like I was hoping to do at line #60? I would like to show form errors as
something like:
Sorry...but the following items were incorrect.
Age: Cannot be an integer.
Age: Missing value.
Name: Missing value.
I would like to get the name of each element in my TT template via
[% element.label %] like so:
[% IF result.have_errors %]
<p><b>Sorry...but the following items were incorrect.</b><br/>
[% FOREACH element = result.have_errors %]
[% FOREACH error = result.errors(element) %]
[% element.label %] : [% error.message %]<br/>
[% END %]
[% END %]
</p>
[% END %]
Is this possible?
38 sub formA : Global {
39 my ($self, $c) = @_;
40
41 my $params = $c->request->params;
42
43 # create form A
44 my $formA = $c->widget('formA')->method('post')->action('formA');
45 $formA->element('Textfield', 'age' )->label('Age')->size(3);
46 $formA->element('Textfield', 'name')->label('Name')->size(60);
47 $formA->element('Submit', 'ok' )->value('OK');
48
49 # Add some constraints
50 $formA->constraint('Integer', 'age' )->message('Must be
integer.');
51 $formA->constraint('Not_Integer', 'name')->message('Cannot be an
integer.');
52 $formA->constraint('All', 'age', 'name')->message('Missing
value.');
53
54 # if form A was submitted...
55 if ($formA->indicator('ok') eq 'formA' && $params->{ok} eq 'OK') {
56 $c->log->info("form A was submitted");
57
58 # get submitted form data
59 my $result = $c->widget_result('formA'); # with query params
60 $formA->empty_errors(0);
61 my @names = $result->has_errors;
62 if (@names) {
63 $c->log->info("form A has errors in: @names");
64 # return form with error messages
65 $c->stash->{template} = 'formA.tt';
66 $c->stash->{result} = $result;
67 }
68 else {
69 $c->log->info("form A is OK");
70 $c->forward('process_formA');
71 }
72 }
73 else {
74 # display form A
75 $c->log->info("display form A");
76 $c->stash->{template} = 'formA.tt';
77 $c->stash->{result} = $formA->process;
78 }
79 }
Thanks for your time
Maurice
More information about the Catalyst
mailing list