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

Monks

I'm writing a form to be updated and the results put into the DB. Generally, it works, but if the DB update fails (or some other code at that point) I'd like to re-display the form, along with a msg. Ideally I'd like to be able to display the msg next to a specific field (if related).
An example code flow from the docs http://www.formbuilder.org/download/CGI-FormBuilder-3.0501/docs/CGI/FormBuilder.html looks like this:

# Check to see if we're submitted and valid if ($form->submitted && $form->validate) { # Get form fields as hashref my $field = $form->fields; # Do something to update your data (you would write this) do_data_update($field->{lname}, $field->{fname}, $field->{email}, $field->{phone}, $field->{gender}); # Show confirmation screen print $form->confirm(header => 1); } else { # Print out the form print $form->render(header => 1); }
However it doesn't handle the situation I described above. I tried to force a data validation error to occur if my own error flag is set, and even activated the debug option, but I don't get any msg on screen and no debug output appears anywhere I can see.
# Check form if( $form->submitted ) { if( $form->validate ) { # More data munging $fields = $form->field; %tidy_fields = do_data_tidy($param_action, %{$fields}); # Amend Database $cfg::err_flag = 0; db_start_txn() if( !$cfg::err_flag ); update_database(%tidy_fields) if( !$cfg::err_flag ); db_commit_txn() if( !$cfg::err_flag ); if( !$cfg::err_flag ) { print $form->confirm(sticky => 1,header => 1); } else { #DEBUG log_msg("mytrap"); # Invalid; show form and allow corrections $cfg::err_flag = 0; $form->field(name => 'new_status', message => "Uknown change requested" ); $form->validate(new_status => 'NONE'); print $form->render(sticky => 1, debug =>2); } } else ....
FYI, I (very) rarely do CGI of any type and I've never used CGI::FormBuilder before, so please answer in simple terms.

Thx
Chris

Replies are listed 'Best First'.
Re: CGI::FormBuilder & custom msgs
by olus (Curate) on Feb 01, 2008 at 11:49 UTC

    You must validate the return of the database update so that you can check if there were any errors. If there were errors then render the form again with the error messages on the comment element of the fields hash, else show the confirmation page.
    You are not checking for errors on do_data_update, just assuming all is well and show the confirmation screen.

    Now, to display a message next to the field that may have caused the error, you will need to parse the error message you get, but these messages may not be descriptive enough to let you know which field caused the error.

    One thing you must do before sending the data to the database is to validate all the values you get upon form submition and build a data structure that keeps track of the invalid fields. If all values are valid then issue the db update. If not, re-build the form adding comments on the invalid fields and render it. This way it will be less probable to get an error from the db that is related to the fields.

      I am checking the DB etc, that's what the $cfg::err_flag var is for. It gets set if any of the subs have a problem. When an error does occur, the prog does go into the relevant block of code, that's what the "log_msg("mytrap");" bit confirms, but as I said, although I set the message option as shown, the form re-renders, but the msg does NOT appear and the debug option (which I also set) does not seem to produce any output.