[html-formfu] How to best pass the resultset object to DBIC validator callbacks?

Dan Dascalescu ddascalescu+formfu at gmail.com
Wed Mar 11 08:34:52 GMT 2009


> And the class would be something like this:
>
>    package HTML::FormFu::Validator::MyApp::UniqueURL;
>    use strict;
>    use warnings;
>    use base 'HTML::FormFu::Validator';
>
>    sub validate_value {
>        my ( $self, $value, $params ) = @_;
>
>        my $c = $self->form->stash->{context};
>
>        return $c->model('DBIC::Urls')->validator_unique_url($value);
>    }
>
>    1;

Thanks Carl, that looks like a great solution. There's only one thing
I need to figure out before I can ditch the callback approach:

How to pass a custom error message to the validator?

Right now I have:

    my $validator_alias = $field_alias->get_validator({ type => 'Callback' });
    $validator_alias->callback(sub{
        my $value = shift;
        return 1 if $c->model('DIBC::Urls')->validator_unique_alias($value);
        # here, validation failed
        my $existing_url_full = $c->model('DIBC::Urls')->find({alias
=> $value})->url_full;
        # add a hyperlink to the full URL in the error message
        $validator_alias->message(
            $validator_alias->message . ": <a
href=\"$existing_url_full\">" . $c->req->base . $value . '</a>'
        );
        return 0;
    });

Could validators return their own error messages somehow?

Thanks,
Dan



More information about the HTML-FormFu mailing list