[ Possibly related but old thread: http://thread.gmane.org/gmane.comp.lang.perl.modules.html-formfu.general/415/ ]
In a Catalyst application, I'm trying to implement a validator callback that does a database lookup. Normally, I would do this: In the form config file, have: "validators" : [ { "type": "Callback", "callback": "MyApp::Schema::ResultSet::Urls::validator_unique_url", "message": "This URL already exists" } ] Then in MyApp::Schema::ResultSet::Urls, use base qw/MyApp::Schema::Base::ResultSet/; sub validator_unique_url { my $value = shift; .... how do I get at the DB? } The problem here is that I don't have $elf and I can't call __PACKAGE__->find or other resultset methods. That would generate this error: Can't use string ("MyApp::Schema::ResultSet::Urls") as a HASH ref while "strict refs" in use at C:/strawberry/perl/site/lib/Class/Accessor/Grouped.pm line 246. The workaround that I found was to add the callback programmatically and pass it an anonymous sub which in turn calls the real validator MyApp::Schema::ResultSet::Urls::validator_unique_url. Here's the code: sub index :Path :Args(0) :FormConfig { my ( $self, $c ) = @_; my $form = $c->stash->{form}; my $field = $form->get_all_element({name => 'url'}); $field->validator('Callback')->callback(sub{ my $value = shift; return $c->model('DBIC::Urls')->validator_unique_url($value); }); 1. Is this the best way to achieve the goal of doing some DB-related validation in a callback? If so, I'll be happy to document this in the Validator::Callback.pm, which is painfully empty at the moment. 2. Is it best practice to put the callback information in the form config file, or to modify the form in the controller code? Thanks, Dan PS: after an hour of working with this, the anonymous sub approach feels very natural and almost not worth documenting - but when I hit it the first time, I wish there were some documentation _______________________________________________ HTML-FormFu mailing list HTML-FormFu@lists.scsys.co.uk http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu