[Catalyst] HTML::Widget forms

Carl Franks fireartist at gmail.com
Thu Mar 2 09:58:15 CET 2006


quoth the pod:
"
empty_errors()
After validation, if errors are found, a span tag is created with the
id "fields_with_errors".
Set this value to cause the span tag to always be generated.
"
It doesn't do what you think it does.

I _think_ you can get the result you want by editing the css stylesheet.
Edit the .labels_with_errors style, so it just reads
.labels_with_errors {
  display: none;
}
And delete the .error_messages style.

Carl


On 02/03/06, Maurice Height <mauriceh at bigpond.net.au> wrote:
> 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
>
>
>
> _______________________________________________
> Catalyst mailing list
> Catalyst at lists.rawmode.org
> http://lists.rawmode.org/mailman/listinfo/catalyst
>



More information about the Catalyst mailing list