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

I'm doing some meat& drink type CGI programming. Presenting a user with a table containing fields, like the following :
... table({-border=>1}, th ({-colspan=>2},"<H3>Please verify/ correct the info +rmation below."), Tr (td({-align=>'right'},["First Name"]),td({-align=>' +left'},[ textfield(-name=>'user_firstname' ,-default=>$userinfo[0])]) +), ....
and, I'd like the "First Name" bit to show up in red if it's blank. I came up with
Tr (td({-align=>'right'},[($state==2?"<FONT COLOR=FF0000>":undef)."Fir +st Name".($state==2?"<FONT COLOR=FF0000>":undef)]),td({-align=>'left' +},[ textfield(-name=>'user_firstname' ,-default=>$userinfo[0])])),
where $state is a variable containing an errorlevel from a validation routine. In this case, $state==2 means that the user name was blank. It seems like there could be a multitude of less cumbersome ways to do this -- what's AWTDI?

Replies are listed 'Best First'.
Re: Optional FONT tag
by Blue (Hermit) on Jan 05, 2001 at 00:39 UTC
    For a non-Perlish answer, have you looked at cascading style sheets? You could define a style for your "errored" boxes and it would automatically be applied. Much less reliance on HTML within your code.

    I noticed that you are using a specific error message for first name blank. Something you may want to consider if you can have several errors of this type (first name blank, last name blank, etc.) is to set bits in your error collection routine instead of just a single return code, and then set up a generic routine for your output which checks the correct bit and puts in you-must-enter-this-red if it's flagged.

    BTW, why are you setting the font a second time? With your current logic, shouldn't the second <FONT COLOR=FF0000> be </FONT> instead?

    Good luck,
    =Blue
    ...you might be eaten by a grue...

      The preferred way to avoid the <FONT> tag and take advantage of styles sheets is to use <SPAN>. And, as luck would have it, CGI.pm supports span (if you import the :html3 definitions).
      use CGI qw(:standard :html3); ... print span({-style => 'Color: red;'}, "I'm an error value!");
      Or, to do this with style sheets:
      use CGI qw(:standard :html3); ... print span({-class => 'Error'}, "I'm an error value!");
      I'll leave getting the style sheet defined as an open-book excercise. Reading the CGI.pm POD is allowed and encouraged.

      Hear, hear! CSS is the way to go!

      (Seeing this reminded me that I need to update the "pet peeves section" of my home node)

Re: Optional FONT tag
by turnstep (Parson) on Jan 05, 2001 at 00:37 UTC
    my $color = ($state==2) ? "#000000" : "#FF0000"; ##... qq!<FONT COLOR="$color">"First Name"</FONT>!

    Some may point out that the use of the FONT tag is discouraged. If you do use it, make sure you quote all values, such as the color above. Just OWTDI, there are other better, more expandable ones. Can we assume that different values of state need different sections of the form to be "red-highlighted"?

    This is also a good job for javascript. Use javascript to catch most people, and have perl as a backup for those people who have js turned off or thos who have circumvented it.

Re: Optional FONT tag
by c-era (Curate) on Jan 05, 2001 at 00:44 UTC
    Here are a couple of ideas, first instead of making the font tag optional, just make the color optional, this will let you use a close font tag and only one compairison.
    Tr (td({-align=>'right'},["<FONT ".($state==2?"COLOR=FF0000":undef)."> +First Name</FONT>"]),td({-align=>'left'},[ textfield(-name=>'user_fir +stname' ,-default=>$userinfo[0])])),
    Since you are only doing one compairison you could even call your sub from within the statement.
    Tr (td({-align=>'right'},["<FONT ".(val_sub($userinfo[0])==2?"COLOR=FF +0000":undef).">First Name</FONT>"]),td({-align=>'left'},[ textfield(- +name=>'user_firstname' ,-default=>$userinfo[0])])),
    Another variation that I like to use is to change the background color of both cells instead of the font color of one cell (users can find the errors quicker). I hope this gives you some ideas and that other people will post how they have done this.

    P.S. The above code is untested (but I did look at my previous work).