I'm using HTML::FormHandler with Catalyst and I have this field:
has_field 'client_account_id' => ( type => 'Select', options_method =>
\&options_account_id);
I have 3 tables that are connected with foreign keys:
clients client_accounts login
------- --------------- -----
id client_id client_account_id
Now I want &options_account_id
to fill the client_account_id field with client_accounts only for a certain client_id. Here is the method I have so far:
sub options_account_id {
use my_app;
my $self = shift;
my @client_accounts = my_app->model('DB::ClientAccount')->search({
'client_id' => $client_id},
{
select => [ qw/id domain/ ], ## SELECT
})->all;
my @options;
for(@client_accounts) {
push @options, { value => $_->id, label => $_->domain};
}
return @options;
}
Now it doesn't work obviously because the $client_id variable does not exist. My question is, is there a way to somehow pass in the certain client id when I create the new form? Or does anyone know of a better way to do this? Thanks!
provide client_id in a form constructor inside controller:
my $form = MyApp::Form::MyFormPage->new( client_id => $some_id);
add attribute in the form class MyApp::Form::MyFormPage
:
has 'client_id' => ( is => 'rw' );
Access this attribute in your method:
sub options_account_id {
my $self = shift; # self is client_account_id field so has no client_id method
my $clientid = $self->form->client_id; # access parent to get client id
}
If you already solved this, could you please share your solution? So far I could find this approach:
Pass Catalyst context to the form object (although, this should be avoided per http://metacpan.org/pod/HTML::FormHandler::Manual::Catalyst#The-Catalyst-context ) and then query context for passed form parameters. And form itself would set these parameters dynamically based on client_id.
Although this approach mixes MVC and I do not like it. So if you did find better solution - please let me know.
ps: BTW, glad to see Catalyst dev from Austin!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.