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


In reply to Error management with CGI::FormBuilder and Template::Toolkit by LesleyB

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.