2009-12-19 12 views
0

我假設你可以在HTML :: FormFu中使用XML :: Simple,因爲FromFu使用Config :: Any來加載它的配置數據。如何使用XML :: Simple來配置HTML :: FormFu?

但是,我似乎無法找到任何XML :: FormFu使用示例XML配置。我不僅得到一個錯誤。我不確定我的xml結構是否正確以創建所需的表單。例如,在選項上,formfu需要一個數組參考數組。但我很確定這個XML會產生一個哈希引用數組。

我沒有做正確的事情......這是我的XML文件的開頭:

<?xml version="1.0" encoding="utf-8" ?> 
<config> 
    <indicator>submit</indicator> 
    <elements> 
    <element type="FieldSet" name="overrides" label="Over Rides"> 
    <attributes title="Use these fields to override the csv value with this constant value" /> 
     <elements> 
     <element type="text" name="client" label="Client" /> 
     <element type="Select" name="bid_type" label="Bid Type"> 
     <options bid="Bid" /> 
     <options approved="Approved" /> 
    </element> 
    <element type="text" name="client_pay" label="Client Pay" /> 
    <element type="text" name="due_date" label="Due Date" /> 
    <element type="text" name="start_date" label="Start Date" /> 
    <element type="Radiogroup" name="category" label="Category"> 
     <options grass_cut_initial="Grass Cut - Initial"/> 
     <options grass_cut_recut="Grass Cut - Recut"/> 
     <options secure="Secure"/> 
     <options winterization="Winterization"/> 
     <options rehab="Rehab" /> 
     <options custom="Custom"/> 
    </element> 
    <element type="text" name="contractor" label="Contractor" /> 
    <element type="text" name="contractor_pay" label="Contractor Pay" /> 
    </elements> 
</element> 

我得到這個錯誤:

 
[debug] Catalyst::Controller::HTML::FormFu::Action::FormConfig loading config file 'workorders/import' 
[error] Caught exception in myapsjobs::Controller::WorkOrders->import "Error parsing /home/jon/aps-dev/myapsjobs/root/forms/workorders/import.xml: /home/jon/aps-dev/myapsjobs/root/forms/workorders/import.xml:38: parser error : Premature end of data in tag config line 1 
at /usr/local/share/perl/5.10.0/HTML/FormFu/ObjectUtil.pm line 502" 
+0

是否有某些特定原因希望使用XML?例如,你是否從其他工具動態創建配置? – 2009-12-23 02:32:33

回答

2

試圖創建一個XML文件, XML::Simple將解析成特定的數據結構可能是一個真正的痛苦。我發現處理這個問題最簡單的方法是從你想要的數據結構開始,通過XMLout運行它,然後根據需要修改生成的XML。

use strict; 
use warnings; 
use XML::Simple; 

my $config = { 
    'indicator' => 'edit', 
    'elements' => [ 
     { 
      'name' => 'overrides', 
      'label' => 'Over Rides', 
      'type' => 'Fieldset', 
      'attributes' => { 
       'title' => 'Use these fields to override the csv value with this constant value', 
      }, 
      'elements' => [ 
       { 
        'type' => 'text', 
        'name' => 'client', 
        'label' => 'Client', 
       }, 
       { 
        'type' => 'Select', 
        'name' => 'bidy_type', 
        'label' => 'Bid Type', 
        'options' => [ 
         [ 'bid' => 'Bid' ], 
         [ 'approved' => 'Approved' ], 
        ], 
       }, 
       { 
        'type' => 'text', 
        'name' => 'client_pay', 
        'label' => 'Client Pay', 
       }, 
       { 
        'type' => 'text', 
        'name' => 'due_date', 
        'label' => 'Due Date', 
       }, 
       { 
        'type' => 'text', 
        'name' => 'start_date', 
        'label' => 'Start Date', 
       }, 
       { 
        'type' => 'Radiogroup', 
        'name' => 'category', 
        'label' => 'Category', 
        'options' => [ 
         [ 'grass_cut_initial' => 'Grass Cut - Initial' ], 
         [ 'grass_cut_recut' => 'Grass Cut - Recut' ], 
         [ 'secure' => 'Secure' ], 
         [ 'winterization' => 'Winterization' ], 
         [ 'rehab' => 'Rehab' ], 
         [ 'custom' => 'Custom' ], 
        ], 
       }, 
       { 
        'type' => 'text', 
        'name' => 'contractor', 
        'label' => 'Contractor', 
       }, 
       { 
        'type' => 'text', 
        'name' => 'contractor_pay', 
        'label' => 'Contractor Pay', 
       }, 
      ], 
     }, 
    ], 
}; 
my $xml = XMLout($config, 'KeyAttr' => []); 

print "$xml\n"; 

結果

<opt indicator="edit"> 
    <elements label="Over Rides" name="overrides" type="Fieldset"> 
    <attributes title="Use these fields to override the csv value with this constant value" /> 
    <elements label="Client" name="client" type="text" /> 
    <elements label="Bid Type" name="bidy_type" type="Select"> 
     <options> 
     <anon>bid</anon> 
     <anon>Bid</anon> 
     </options> 
     <options> 
     <anon>approved</anon> 
     <anon>Approved</anon> 
     </options> 
    </elements> 
    <elements label="Client Pay" name="client_pay" type="text" /> 
    <elements label="Due Date" name="due_date" type="text" /> 
    <elements label="Start Date" name="start_date" type="text" /> 
    <elements label="Category" name="category" type="Radiogroup"> 
     <options> 
     <anon>grass_cut_initial</anon> 
     <anon>Grass Cut - Initial</anon> 
     </options> 
     <options> 
     <anon>grass_cut_recut</anon> 
     <anon>Grass Cut - Recut</anon> 
     </options> 
     <options> 
     <anon>secure</anon> 
     <anon>Secure</anon> 
     </options> 
     <options> 
     <anon>winterization</anon> 
     <anon>Winterization</anon> 
     </options> 
     <options> 
     <anon>rehab</anon> 
     <anon>Rehab</anon> 
     </options> 
     <options> 
     <anon>custom</anon> 
     <anon>Custom</anon> 
     </options> 
    </elements> 
    <elements label="Contractor" name="contractor" type="text" /> 
    <elements label="Contractor Pay" name="contractor_pay" type="text" /> 
    </elements> 
</opt> 

不完全是XML,你所期望的,但它能夠完成任務。您也可以仔細檢查這一工程運行回通過XMLin和檢查結果數據結構:

use strict; 
use warnings; 
use XML::Simple; 
use Data::Dumper; 

my $xml = '...'; 
my $config = XMLin($xml, 'KeyAttr' => []); 
print Dumper($config); 

我使用KeyAttr選項的原因是因爲這個caveat

If you wish to 'round-trip' arbitrary data structures from Perl to XML and back to Perl, then you should probably disable array folding (using the KeyAttr option) both with XMLout() and with XMLin().

而且,我似乎無法找到一種方法將選項傳遞給Config::Anyload_config_file(儘管我沒有花太多時間搜尋HTML::FormFu的文檔)。這意味着您可能必須自己使用XML::Simple才能將數據結構傳遞給populate


正如您所看到的,在使用HTML :: FormFu時,XML配置文件確實不是最簡單的方法。如果你對其他方法開放,我會建議使用一些更好的映射到Perl數據結構的東西,比如YAML(可能是它在documentation examples中使用的原因之一)。就個人而言,我只是使用Perl來創建我的表單並將代碼/配置文件粘貼到模塊中。

相關問題