[英]catalyst html::formhandler pass form value
I'm using HTML::FormHandler with Catalyst and I have this field: 我正在使用HTML :: FormHandler和Catalyst,我有这个字段:
has_field 'client_account_id' => ( type => 'Select', options_method =>
\&options_account_id);
I have 3 tables that are connected with foreign keys: 我有3个表与外键连接:
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. 现在我想&options_account_id
只为某个 client_id用client_accounts填充client_account_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. 现在它显然不起作用,因为$ client_id变量不存在。 My question is, is there a way to somehow pass in the certain client id when I create the new form? 我的问题是,当我创建新表单时,有没有办法以某种方式传递某个客户端ID? Or does anyone know of a better way to do this? 或者有人知道更好的方法吗? Thanks! 谢谢!
provide client_id in a form constructor inside controller: 在控制器内部的表单构造函数中提供client_id:
my $form = MyApp::Form::MyFormPage->new( client_id => $some_id);
add attribute in the form class MyApp::Form::MyFormPage
: 在表单类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. 将Catalyst上下文传递给表单对象(但是,应该避免这种情况,根据http://metacpan.org/pod/HTML::FormHandler::Manual::Catalyst#The-Catalyst-context ),然后查询传递的表单参数的上下文。 And form itself would set these parameters dynamically based on client_id. 表单本身将根据client_id动态设置这些参数。
Although this approach mixes MVC and I do not like it. 虽然这种方法混合了MVC,但我不喜欢它。 So if you did find better solution - please let me know. 所以如果你确实找到了更好的解决方案 - 请告诉我。
ps: BTW, glad to see Catalyst dev from Austin! ps:BTW,很高兴看到来自Austin的Catalyst dev!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.