2009-12-19 2 views
0

FromFu가 Config :: Any를 사용하여 구성 데이터를로드하므로 XML :: Simple with HTML :: FormFu를 사용할 수 있다고 가정합니다.XML :: Simple을 사용하여 HTML :: FormFu를 구성하는 방법은 무엇입니까?

그러나 HTML :: FormFu와 함께 사용되는 샘플 xml 구성을 찾지 못하는 것 같습니다. 뿐만 아니라 오류가 발생합니다. 내 XML 원하는 양식을 만들려면 제대로 구성되어 있는지 모르겠습니다. 예를 들어, 옵션에서 formfu는 배열 참조의 배열을 원합니다. 하지만 난 정말이 XML 해쉬 refs의 배열을 생성합니다 확신 해요.

<?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> 

나는이 오류 받고 있어요 : 내가 바로 뭔가를하고 있지 않다

... 여기 내 XML 파일의 시작에 XML 파일을 만들려고

 
[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을 사용하려는 특별한 이유가 있습니까? 예를 들어, 다른 도구에서 configs를 동적으로 작성하고 있습니까? –

답변

2

을 그 XML::Simple 특정 데이터 구조로 구문 분석하는 것은 정말 고통 스러울 수 있습니다. 이것을 처리하는 가장 쉬운 방법은 원하는 데이터 구조로 시작하여 XMLout을 통해 실행 한 다음 결과 XML을 적합하게 수정하는 것입니다.

<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> 

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"; 

결과는 정확히 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().

, 나는 HTML::FormFu에 대한 문서를 통해 많은 시간을 들여 보지 않고 load_config_file을 통해 Config::Any에 옵션을 전달할 수있는 방법을 찾지 못하는 것 같습니다. 즉, 데이터 구조를 populate으로 전달하려면 직접 XML::Simple을 사용해야 할 수도 있습니다.


실제로 볼 수 있듯이 XML 구성 파일은 HTML :: FormFu로 작업 할 때 실제로 가장 쉬운 방법이 아닙니다. 다른 접근 방식에 대한 질문이 있다면 YAML (아마도 documentation examples에서 사용 된 이유 중 하나 일 것입니다.)과 같이 Perl 데이터 구조에 훨씬 더 잘 매핑 된 것을 사용하는 것이 좋습니다. 개인적으로 필자는 Perl을 사용하여 양식을 만들고 코드/구성을 모듈에 고정합니다.

관련 문제