LesleyB has asked for the wisdom of the Perl Monks concerning the following question:

Greetings oh wise ones

Unfortunately the FB mailing list is down and doesn't look like it will be coming up soon. I have searched the list archives and gotten some clues there but I feel sure others may have wandered this route too and may be able to offer solutions to my problem.

I'm using TT2 templates with FB, and I do server side error checking. I am not using the Javascript provided in FB for form validation.

Using FB without a template thus

#!/usr/bin/perl -wT 2 3 4 use strict; 5 use CGI; 6 use CGI::Carp qw(fatalsToBrowser); 7 use CGI::FormBuilder; 8 9 my @fields = qw(name email password confirm_password zipcode); 10 11 my $form = CGI::FormBuilder->new( 12 fields => \@fields, 13 header => 1, 14 validate => { 15 name => 'NAME', 16 email => 'EMAIL', }, 17 method => 'post', 18 19 ); 20 if ($form->submitted && $form->validate) { 21 # print confirmation screen 22 print $form->confirm; 23 } else { 24 # print the form for them to fill out 25 print $form->render; 26 }

gives the default behaviour of highlighting the fields listed in the validate hash and put up a message saying the highlighted fields are required.

I have tried using the validate option with my TT2 template but, as I have written my TT2 code like this

<div class="formelement"><div class ="label"> [% contact.field.yr_name.label %]</div> [% contact.field.yr_name.field %]</div>

because I don't like the default HTML provided by FB, I don't get the validate display - highlighting and 'Invalid entry' message.

I am wondering how to recreate this particularly when re-rendering the form after an error has occurred. I would like to at least say an error has occurred with the form displayed, or, more preferably, go the full hog and mark the fields that are in error so the user can correct them.

I've tried passing a hidden field to the template by defining this hash

77 my %myf_error = ( 78 name => 'myf_error', 79 id => 'myf_error', 80 type => 'hidden', 81 values => '0', 82 );

with this object declaration

85 my $form = CGI::FormBuilder->new( 86 template => { 87 type=> 'TT2', 88 template => 'contact.tt', 89 variable => 'contact', }, 90 name => 'contactform', 91 id => 'contactform', 92 #fields => \@formfields , 93 method => 'post', 94 action => $ENV{SCRIPT_NAME}, 95 keepextras => 1, 96 );

I use statements of the form $form->field( %myf_error); to declare the fields into the form. This allows me to use labels for text input fields, select lists and textarea boxes.

Even with

16 [% FOREACH val IN contact.field.myf_error.values %] 17 [% val %] 18 [% END %]

in the template code, I don't seem to be able to get a hidden field to pass through or read its values.

The template code

[% contact.field.myf_error.values %]

displays

ARRAY(0x8681740)

while the template code [% contact.field.notexisting.values %] displays nothing at all - as per the TT2 documentation. So I deduce something is getting through.

If I try [% contact.field.myf_error.field %] I get a text type input field displayed, not a hidden type.

So I guess my questions are :

  1. what methodology can I use to report errors in form submission using FB and TT2? (I'd been thinking set a value of a hidden field and then use it in the template but I don't seem to be able to get the hidden value out on the template side.)
  2. how do I pass a hidden field from FB to a TT2 template?

Any pointers or comments on my code and methodology would be also appreciated.

Thanks in advance

UPDATE Hidden fields working without a change to the CGI code with v3.05 - I at least now get a hidden fieldtype instead of a text fieldtype when displaying the field in the template - but they don't appear to work with at all with v3.03 - despite the existence of CGI::FormBuilder::Field::hidden. /UPDATE

Replies are listed 'Best First'.
Re: Error management with CGI::FormBuilder and Template::Toolkit
by Anonymous Monk on Aug 04, 2008 at 13:31 UTC
    Documentation shows an "invalid" attribute
    [% IF field.invalid %] Missing or invalid entry, please try again.

      I searched through FB's documentation via perldoc and at the site and couldn't find anything on

      [% field.invalid %].

      Would you please provide a reference of where you found that information?

Re: Error management with CGI::FormBuilder and Template::Toolkit
by Anonymous Monk on Aug 04, 2008 at 13:33 UTC
    Just so you know, no need to manually add line numbers, those can be added through user settings, and it doesn't affect "download code"

      Thanks for the useful hint, AM, but I'm using vi with :set number.

      Do I need to :set nonumber before grabbing the code and pasting it in ?

        I think so (don't use vi, so not sure), otherwise someone wanting to run your code would have to filter out the line number first