[html-formfu] Suppressing error message output?

Carl Franks fireartist at gmail.com
Wed May 6 10:22:58 GMT 2009


2009/5/6 Matthias Dietrich <perl at rainboxx.de>:
> Hi
>
> I currently playing around with FormFu.  I want to display the error
> messages at the top of a form like: "The following fields are required:
> Field1, Field2".  I looked at the docs but there doesn't seem to be a
> possibility to let FormFu do that, so I need to grab all the errors, look
> what error occured and on which field.  After collecting the information,
> I'm able to display that combined error message.  But I don't see a way to
> suppress the error message output near the labels.  Is there a way round?
>
> May be there is a better solution for my issue, I'm pleased to hear any
> suggestions!
>
> I'm using Catalyst with FormFu and Mason, not TT.  If it's possible I want
> to avoid using TT templates because I'm already using Mason and don't want
> to install the complete TT stuff on my live machine.

Hmm, not easy - I'd normally suggest looking at t/examples/ for custom
rendering, but the render_field() and render_label() methods require
TT.

I'd suggest inspecting the field errors to build your error message.
Something like this (as a very rough example):

    my $error_message = '';
    for my $field ( @{ $form->get_fields } ) {
        my @errors = @{ $field->get_errors({ forced => 1 });
        next if !@errors;
        $error_message .= sprintf '<p>field %s has errors</p><ul>', (
$field->label || $field->name );
        for my $error (@errors) {
            $error_message .= sprintf '<li>%s</li>', $error->message;
        }
        $error_message .= '</ul>';
    }

To get it displayed at the top of the form:

    # use _xml() variant as our message contains xhtml tags
    $form->form_error_message_xml( $error_message );

And clear the errors attached to the fields:

    $form->clear_errors;

And to get the top error message to display now that the form has no errors:

    $form->force_error_message( 1 );

This /should/ work - but I've never tried anything like it before.

Carl



More information about the HTML-FormFu mailing list