2009/3/11 Dan Dascalescu <ddascalescu+for...@gmail.com>:
>> 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?

Instead of returning 0 on error, do:

    die HTML::FormFu::Exception::Validator->new({
        message => 'foo',
    });

This won't modify the validator's message, only that specific error's.
Hope that's what you're after.

Carl

_______________________________________________
HTML-FormFu mailing list
HTML-FormFu@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu

Reply via email to