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

Wolfgang Kinkeldei wolfgang at kinkeldei.de
Thu Jun 4 20:15:37 GMT 2009


Hi,

Am 04.06.2009 um 21:40 schrieb Ascii King:

> This Validator references a bit of code (validator_unique_url) that  
> presumably calls the database to check to see if the value is  
> unique. Would someone be able to show what this code would actually  
> look like? I have the rest of the validator example from this thread.
>> 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?
>> }

I recently did almost the same and it works well. My relevant part of  
the form looks like this:

   - type: Text
     name: login
     label: Login
     constraints:
       - Required
       - type: Callback
         callback: 'DemoApp::Controller::Login::login_check'
         message: Already in use
     filters:
       - TrimEdges

my callback-sub is like that -- you can obtain the resultset object  
using your Application class name. DB is my Schema, Person my table.

sub login_check :Private {
     my $value = shift;

     my $result = DemoApp->model('DB::Person')
                         ->search({login => $value})
                         ->single();

     return $result ? 0 : 1;
}

if you look into HTML::FormFu::Validator::Callback, you can see that  
the validator coderef is called with the entered value only.


> Also, in the following sub, what would be an example of a context?  
> Would it be 'MyApp::Schema' or 'MyApp::Schema::TableName'?


>
>>>   sub validate_value {
>>>       my ( $self, $value, $params ) = @_;
>>>
>>>       my $c = $self->form->stash->{context};
>>>
>>>       return $c->model('DBIC::Urls')->validator_unique_url($value);
>>>   }



Regards,

Wolfgang Kinkeldei

-- 

' /\_/\ ' .print[split??,"".(($/=q|Cms)+-03467:;<=|)=~tr!C-z -B! -z!)x
'( o.o )' .$/]->[hex]foreach split qr<>,qq+1ecd039ad65b025b8063475b+||
' > ^ < ' .q<!-- Wolfgang Kinkeldei - mailto:wolfgang at kinkeldei.de -->



-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2425 bytes
Desc: not available
Url : http://lists.scsys.co.uk/pipermail/html-formfu/attachments/20090604/a0f1bced/smime.bin


More information about the HTML-FormFu mailing list