Mercurial > pub > Impl
annotate Lib/IMPL/Web/TT/Form.pm @ 145:bd10093bb122
Minor changes
author | wizard |
---|---|
date | Wed, 21 Jul 2010 06:27:12 +0400 |
parents | b56ebc31bf18 |
children | e6447ad85cb4 |
rev | line source |
---|---|
122 | 1 use strict; |
2 package IMPL::Web::TT::Form; | |
3 | |
4 use base qw(IMPL::Web::TT::Control); | |
5 | |
124 | 6 use IMPL::Class::Property; |
7 use IMPL::DOM::Navigator::SchemaNavigator; | |
126 | 8 __PACKAGE__->PassThroughArgs; |
124 | 9 |
10 BEGIN { | |
11 public property base => prop_all; | |
12 public property schema => prop_all; | |
13 public property errors => prop_all; | |
14 public property data => prop_all; | |
138 | 15 public property state => prop_all; |
16 public property formResult => prop_all; | |
124 | 17 } |
18 | |
19 sub CTOR { | |
20 my ($this) = @_; | |
21 | |
138 | 22 if (my $form = $this->formResult) { |
23 $this->base($form->{formName}); | |
24 $this->errors($form->{formErrors}); | |
25 $this->data($form->{formData}); | |
26 $this->schema($form->{formSchema}); | |
27 $this->state($form->{state}); | |
28 } else { | |
124 | 29 |
138 | 30 $this->base($this->nodeName) unless $this->base; |
31 | |
32 die new IMPL::InvalidArgumentException('A schema is required for a form',$this->nodeName) | |
33 unless eval { $this->schema->isa( typeof IMPL::DOM::Schema ) }; | |
126 | 34 |
138 | 35 die new IMPL::InvalidOperationException('Can\'t find a form definition in a schema',$this->nodeName,$this->base) |
36 unless $this->schema->selectNodes(sub { $_->nodeName eq 'ComplexNode' and $_->name eq $this->base }); | |
37 } | |
38 | |
126 | 39 $this->errors([]) unless $this->errors; |
124 | 40 } |
41 | |
126 | 42 sub makeControlArgs{ |
43 my ($this,$path) = @_; | |
124 | 44 |
45 my $navi = new IMPL::DOM::Navigator::SchemaNavigator($this->schema); | |
126 | 46 my @path = ($this->base, split /\./,$path); |
124 | 47 |
126 | 48 $navi->NavigateName($_) or die new IMPL::InvalidArgumentException( |
124 | 49 "Can't find a definition for an element", |
50 $_, | |
51 $path, | |
126 | 52 $this->element, |
53 ) foreach @path; | |
124 | 54 |
55 my $schema = $navi->Current; | |
127
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
56 my $sourceSchema = $navi->SourceSchemaNode; |
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
57 my $queryParameter = join '/', @path; |
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
58 shift @path; |
138 | 59 my $node = $this->data ? $this->data->selectSingleNode(@path) : undef; |
126 | 60 |
61 my @errors; | |
62 | |
63 if ($node) { | |
144
b56ebc31bf18
Empty nodes no more created while transforming a post request to the DOM document
wizard
parents:
138
diff
changeset
|
64 @errors = grep { ($_->Node || $_->Parent) == $node } @{$this->errors}; |
b56ebc31bf18
Empty nodes no more created while transforming a post request to the DOM document
wizard
parents:
138
diff
changeset
|
65 } else { |
b56ebc31bf18
Empty nodes no more created while transforming a post request to the DOM document
wizard
parents:
138
diff
changeset
|
66 @errors = grep $_->Schema == $sourceSchema, @{$this->errors}; |
126 | 67 } |
68 | |
69 return { | |
70 schema => $schema, | |
127
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
71 sourceSchema => $sourceSchema, |
126 | 72 errors => \@errors, |
73 data => $node, | |
127
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
74 nodeValue => $node && $node->nodeValue, # small hack set a non dom class property through |
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
75 queryParameter => $queryParameter |
126 | 76 }; |
77 } | |
78 | |
145 | 79 sub makeContent { |
80 my ($this,$mappings) = @_; | |
81 | |
82 my $formSchema = $this->schema->selectSingleNode(sub { $_->nodeName eq 'ComplexNode' and $_->name eq $this->base } ) | |
83 or die new Exception("Cant find a schema element for the specified form", $this->base); | |
84 | |
85 my $doc = $this->document; | |
86 foreach my $itemSchema ( $formSchema->content->childNodes ) { | |
87 my $itemName = $itemSchema->name; | |
88 if (my $controlClass = $mappings->{$itemName} ) { | |
89 my $contorl = $doc->CreateControl($itemName,$controlClass,$this->makeControlArgs($itemName)); | |
90 $this->appendChild($contorl); | |
91 } | |
92 } | |
93 return; | |
94 } | |
95 | |
126 | 96 sub formErrors { |
97 my ($this) = @_; | |
98 | |
127
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
99 if (my $node = $this->data ) { |
126 | 100 return [ |
101 grep { | |
102 ( $_->Node || $_->Parent) == $node | |
103 } @{$this->errors} | |
104 ]; | |
105 } else { | |
106 return []; | |
107 } | |
124 | 108 } |
109 | |
122 | 110 |
111 1; | |
112 | |
113 __END__ | |
114 | |
115 =pod | |
116 | |
117 =head1 NAME | |
118 | |
119 C<IMPL::Web::TT::Form> - Форма с элементами | |
120 | |
121 =head1 DESCRIPTION | |
122 | |
123 Является элементом управления, тоесть для своего преобразования ипользует | |
124 шаблон | |
125 | |
126 =cut |