2014-10-01 93 views
-2

在Catalyst中使用HTML :: Formhandler我定義了基於數據庫表(item_class)的表單。表單所做的是根據數據庫中幾個表格的內容(不同於表格所基於的表格)設置前三個選擇列表。它看起來像選擇列表都獲得(html)正確的選項。然而,在提交時,我總是得到錯誤:'...'不僅僅是字段的有效值:prg_id和mrk_id。HTML :: Formhandler錯誤:'...'不是有效值

我的錯誤是什麼?任何幫助是極大的讚賞。

我的表單定義:

package BskExt::Form::HMbestellingProducten; 

use HTML::FormHandler::Moose; 
extends 'HTML::FormHandler::Model::DBIC'; 
with 'HTML::FormHandler::Render::Table'; 

use namespace::autoclean; 

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

has '+item_class' => (default => 'NewHmbestellingenproducten'); 

has_field 'wklloc_id' => (type => 'Select', label => 'Winkel(locatie)s'); 
#has_field 'prg_id' => (type => 'Select', label => 'Productgroep', label_column => 'naam', accessor => 'prg'); 
has_field 'prg_id' => (type => 'Select', label => 'Productgroep', error_messages => { select_invalid_value => undef }); 
#has_field 'mrk_id' => (type => 'Select', label => 'Merk', label_column => 'naam', accessor => 'mrk'); 
has_field 'mrk_id' => (type => 'Select', label => 'Merk'); 
has_field 'productnaam' => (type => 'Text', label => 'Product *', size => 32, maxlength => 40, required => 1, messages => { required => 'Verplicht' }, validate_method => \&CatFormFieldValidate::validate_name_ext); 
has_field ' ' => (type => 'Submit', id => 'submit', value => 'Ga door'); 

# 
# The select list for wklloc_id is set by options_wklloc_id which has been omitted from 
# this listing as it made the listing (much) too long 
# 
sub options_mrk_id 
{ 
    my ($self) = shift; 

    my (@mrk_select_list); 

    undef(@mrk_select_list); 
    push @mrk_select_list, { value => '', label => 'Kies eerst een productgroep' }; 

    return @mrk_select_list; 
} 

sub options_prg_id 
{ 
    my ($self) = shift; 

    my (@prg_select_list); 

    undef(@prg_select_list); 
    push @prg_select_list, { value => '', label => 'Kies eerst een winkel(-locatie)' }; 

    return @prg_select_list; 
} 

我的表單模板:

<script> 
    $(function() { 
    $("#wklloc_id").click(function() { 
    $("#prg_id").load("WinkelLocatieProductgroepen?wklloc_id=" + $("#wklloc_id").val()); 
    }); 
    }); 

    $(function() { 
    $("#prg_id").click(function() { 
    $("#mrk_id").load("ProductgroepMerken?wklloc_id=" + $("#wklloc_id").val() + "&prg_id=" + $("#prg_id").val()); 
    }); 
    }); 
</script> 

<p></p> 
[%# Render the HTML::FormHandler Form %] 
[% form.render %] 

(的顯著部分)我的控制器:

sub create : Chained('base') PathPart('create') Args(0) { 
    my ($self, $c) = @_; 

    my $hmbestprod = $c->model('DB::NewHmbestellingenproducten')->new_result({}); 
    return $self->form($c, $hmbestprod,'new'); 
} 

sub form 
{ 
    my ($self, $c, $hmbestprod, $action) = @_; 

    my ($form,$klant_info,$uc_action); 

    $uc_action = uc($action); 
    $klant_info = $c->model('BskExt')->KlantInfo($c->session->{"klt_id"}||0); 
    $form = BskExt::Form::HMbestellingProducten->new(wpl_id => $$klant_info{"wpl_id"}); 
    # Set the template 
    $c->stash(template => 'hmbestellingproducten/form.tt2', action => $uc_action, form => $form); 
    $form->process(
        use_init_obj_over_item => 1, 
        item => $hmbestprod, 
        init_object => { "aantal" => 1 }, 
        params => $c->req->params); 
    return unless $form->validated; 
    # Set a status message for the user & return to list 
    $c->response->redirect($c->uri_for($self->action_for('list'))); 
} 
# 
# This helps jQuery to set the content of the ProductgroepMerken form list 
# 
sub ProductgroepMerken :Chained('base') :PathPart('ProductgroepMerken') :Args(0) 
{ 
    my ($self, $c) = @_; 

    my ($optstr,$prg_id,$productgroepmerken,$wklloc_id); 

    $c->log->debug("ProductgroepMerken\n"); 

    $prg_id = $c->request->params->{"prg_id"}; 
    $wklloc_id = $c->request->params->{"wklloc_id"}; 
# 
# The result: $productgroepmerken contains (at first glance) valid values. These values are not in any way (directly) 
# related to the FORM item_class 'NewHmbestellingenproducten' 
# 
    $productgroepmerken = $c->model ('BskExt')->ProductgroepMerken($wklloc_id,$prg_id); 
    undef($optstr); 
    foreach (@$productgroepmerken) 
    { 
     $optstr .= '<option value="'.$_->{"id"}.'">'.$_->{"naam"}.'</option>'; 
    } 
    $c->log->debug("optstr= $optstr\n"); 
    $c->response->body($optstr); 
} 
# 
# This helps jQuery to set the content of the WinkelLocatieProductgroepen form select list 
# 

sub WinkelLocatieProductgroepen :Chained('base') :PathPart('WinkelLocatieProductgroepen') :Args(0) 
{ 
    my ($self, $c) = @_; 

    my ($optstr,$productgroepen,$wklloc_id); 

    $c->log->debug("WinkelLocatieProductgroepen\n"); 

    $wklloc_id = $c->request->params->{"wklloc_id"}; 
# 
# The result: $productgroepen contains (at first glance) valid values. These values are not in any way (directly) 
# related to the FORM item_class 'NewHmbestellingenproducten' 
# 
    $productgroepen = $c->model ('BskExt')->WinkelLocatieProductgroepen($wklloc_id); 
    $c->log->debug("productgroepen= \n".Dumper($productgroepen)."\n\n"); 
    undef($optstr); 
    foreach (@$productgroepen) 
    { 
     $optstr .= '<option value="'.$_->{"id"}.'">'.$_->{"naam"}.'</option>'; 
    } 
    $c->log->debug("optstr= $optstr\n"); 
    $c->response->body($optstr); 
} 
+1

您可以縮小問題範圍並隔離導致問題的腳本的特定部分嗎?我和其他人可能會被這裏發佈的大量代碼推遲。 – 2014-10-02 07:59:30

+0

對不起。我認爲這可能有助於展示問題的背景。我將刪除多餘的代碼。我的問題與我能夠說出的一樣精確/精確。 – 2014-10-02 13:24:16

+0

當這些選擇字段的選項與表單的結果集集合中的(選擇)字段的字段(定義)不直接相關時,它看起來像使用HTML :: Formhandler選擇字段是不可能的根據。 然而,當模板中提供表單的html時,完全可以做到完全相同的事情。 在我看來,這是一個遺憾,因爲它迫使人們放棄HTML :: Formhandler字段/表單驗證的好功能以及隨之而來的統一用戶界面。沒有(好)的方法來做到這一點? – 2014-10-03 21:33:18

回答

0

的選擇字段對選項驗證爲領域。不要在字段選項中放置空選項,而應使用'empty_select'字段屬性來設置它們:empty_select =>'一些選項標籤'。如果選擇選項應該從數據庫自動生成,則需要使用關係名稱,而不是列名。

相關問題