forms - Catalyst html::formhandler 传递表单值

标签 forms perl catalyst

我正在使用HTML::FormHandler使用 Catalyst,我有这个字段:

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?或者有谁知道更好的方法来做到这一点?谢谢!

最佳答案

在 Controller 内的表单构造函数中提供 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
}

关于forms - Catalyst html::formhandler 传递表单值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11928812/

相关文章:

javascript - 关键依赖: the request of a dependency is an expression on jQuery Form Validator plugin

javascript - AJAX 在页面上提交单个表单

JavaScript 表单验证 : if statement regarded select tag runs even when parameter is false

linux - 如何仅从文件中删除最后一个单词

Perl:使用 HTTP::Request::Common 发布身份验证

perl - 使用 perl 和 Google 的 Geocode API 获取地址的纬度和经度

perl - 在 Catalyst Controller 中处理可选 url 参数的最佳方法是什么?

perl - DBIx::Class 包装/重载列访问器

perl - 如何使用Catalyst上传文件?

javascript - Jquery If条件根据提交或失败显示消息