3

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!

srchulo
  • 5,143
  • 4
  • 43
  • 72

2 Answers2

4

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
}
Alex
  • 414
  • 4
  • 10
0

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!

szabgab
  • 6,202
  • 11
  • 50
  • 64
Pavel Karoukin
  • 5,893
  • 2
  • 16
  • 9