2012-08-13 63 views
3

我使用HTML::FormHandler与催化剂和我有这样的领域:催化剂HTML :: formhandler通形式价值

has_field 'client_account_id' => (type => 'Select', options_method => 
\&options_account_id); 

我有3代表了与外键连接:

clients client_accounts login 
------- --------------- ----- 
id   client_id   client_account_id 

现在我想&options_account_id填充client_account_id字段与client_accounts只为某些 client_id。这里是我到目前为止的方法:

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; 
} 

现在它不工作,因为$ client_id变量不存在。我的问题是,有没有办法在创建新表单时以某种方式传递某个客户端ID?还是有人知道有更好的方法来做到这一点?谢谢!

回答

4

提供的client_id的形式构造内部控制器:

has 'client_id' => (is => 'rw'); 

访问这个属性在你的方法:

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 
} 
+0

太棒了!正是我在找的东西:) – srchulo 2013-01-17 19:27:51

0

如果您已经解决了这个问题,请分享您的解决方案吗?到目前为止,我可以找到这种方法:

将催化剂上下文传递给表单对象(尽管这应该避免每http://metacpan.org/pod/HTML::FormHandler::Manual::Catalyst#The-Catalyst-context),然后查询传递的表单参数的上下文。表单本身会根据client_id动态地设置这些参数。

虽然这种方法混合了MVC,我不喜欢它。所以,如果你找到了更好的解决方案 - 请让我知道。

ps:顺便说一句,很高兴看到奥斯汀的Catalyst开发者!

+0

我在窗体类MyApp::Form::MyFormPage

my $form = MyApp::Form::MyFormPage->new(client_id => $some_id); 

加属性对不起,我从来没有找到方法。但如果你这样做,我会很感激一些帮助;) – srchulo 2012-11-08 07:49:27

+0

如果你没有看到,现在有一个答案,@Pavel。 – srchulo 2013-01-27 04:41:26