catalyst html::formhandler pass form value



我正在使用带有Catalyst的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_id用client_accounts填充client_account_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?或者有谁知道更好的方法来做到这一点?谢谢!

控制器内的表单构造函数中提供client_id:

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

在表单类中添加属性 MyApp::Form::MyFormPage

 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
}

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

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

虽然这种方法混合了 MVC,但我不喜欢它。因此,如果您确实找到了更好的解决方案 - 请告诉我。

ps:顺便说一句,很高兴看到来自奥斯汀的催化剂开发!

最新更新