Zend_Form :: subForm & subForms
Doel is :
$_POST['form']['veldnaam'] = value,
$_POST['subform1']['veldnaam'] = value,
$_POST['subform2']['veldnaam'] = value;
subform1 & 2 zijn dezelfde klassen.
Wat ik nu heb is :
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
$subForm = new Test_Form_Sub();
$subForms = array('a'=>$subForm,'b'=>$subForm,'c'=>$subForm);
$arr = array();
foreach($subForms as $formId => $form){
$subForms[$formId]->setName('form'.$formId);
}
$sub = new Zend_Form_SubForm();
$sub->addSubForms($subForms,'lines');
$this->addSubForm($sub,'sub');
[/code]
Wat als resultaat geeft:
a = sub[veldnaam]
b = sub[ignore][sub][/ignore][veldnaam]
c = sub[ignore][sub][sub][/ignore][price_klm]
Ik weet dat het ergens nodig is om naamgeving van de "parent subform" te negeren of te overschrijven.
Iemand nog ideen of een andere aanpak om gebruik te maken van Zend_SubForm();
(Getest met ZF Versie:1.10)
$subForm = new Test_Form_Sub();
$subForms = array('a'=>$subForm,'b'=>$subForm,'c'=>$subForm);
$arr = array();
foreach($subForms as $formId => $form){
$subForms[$formId]->setName('form'.$formId);
}
$sub = new Zend_Form_SubForm();
$sub->addSubForms($subForms,'lines');
$this->addSubForm($sub,'sub');
[/code]
Wat als resultaat geeft:
a = sub[veldnaam]
b = sub[ignore][sub][/ignore][veldnaam]
c = sub[ignore][sub][sub][/ignore][price_klm]
Ik weet dat het ergens nodig is om naamgeving van de "parent subform" te negeren of te overschrijven.
Iemand nog ideen of een andere aanpak om gebruik te maken van Zend_SubForm();
(Getest met ZF Versie:1.10)
Gewijzigd op 30/05/2010 18:41:53 door PHP hulp
Code (php)
Je weet dat je hier ervoor zorgt dat a, b en c alledrie verwijzen naar hetzelfde subform, en dat je in de foreach lus daaronder effectief 3x $subForm->setName() aanroept op dus dezelfde instantie?
Gewijzigd op 30/05/2010 18:43:32 door Jelmer -
Ik zou denken dat je dan niks wijzigt aan de naam van het SubFormulier, maar als ik voor elk subform een nieuw instantie maak is het resultaat:
a = sub['veldnaam']
b = sub['veldnaam']
c = sub['veldnaam']
Ik kan nog niet echt zeggen wat er mogelijk mis gaat, maar het heeft iets met de referentie te maken. Misschien dan niet van
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
<?php $subForm;
// Maar dan van :
$sub;
// zie:
$sub = new Zend_Form_SubForm();
$sub->addSubForms($subForms,'lines');
$this->addSubForm($sub,'sub');
?>
// Maar dan van :
$sub;
// zie:
$sub = new Zend_Form_SubForm();
$sub->addSubForms($subForms,'lines');
$this->addSubForm($sub,'sub');
?>
Gewijzigd op 30/05/2010 18:56:45 door Andreas Warnaar
Wijzigt Zend_Form_SubForm sowieso iets aan de veldnamen, of zou je daar weer een aparte decorator voor nodig hebben? (je merkt al, ik heb de documentatie niet helemaal gelezen)
PS: Kan je misschien een voorbeeld geven met echte namen ipv het abstracte 'sub' en 'veldnaam'? Ik kan me hier niet bij voorstellen wat er nu eigenlijk uit moet komen.
De magie zit in de setDefaults, isValid en getValues.
Er is een kleine workaround met een prefix voor mijn elementen in de subforms. Deze workaround zorgt er voor dat er geen velden met dezelfde naam voorkomen.
De volgende stap is een Jqueryscript wat een "template subform" kan kopiëren en de attributen wijzigt, zodat je dynamisch subforms kunt toevoegen en kunt verwijderen.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
class Money_Form_Invoice extends Form_Base
{
public function init()
{
$this->setMethod('post')
->setName('invoice')
->setDecorators($this->formDecorators);
$this->addElement('hidden', 'invoice_id', array(
'required' => false,
'decorators' => $this->hiddenDecorator
));
$this->addElement('hidden', 'year', array(
'required' => false,
'decorators' => $this->hiddenDecorator
));
$this->addElement('hidden','serial_code',array(
'required' => true ,
'decorators'=> $this->hiddenDecorator,
));
$this->addElement('text', 'ordernumber', array(
'label' => 'Werkorder',
'maxlength' => 45,
'size' => 15,
'decorators' => $this->elementDecorator,
'required' => true,
'validators' => array(
array('NotEmpty', false, array('messages' => array( 'isEmpty' => 'Dit veld is verplicht')))
),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('text', 'number', array(
'label' => 'Factuur nummer',
'maxlength' => 15,
'size' => 15,
'decorators' => $this->elementDecorator,
'required' => true,
'validators' => array(
array('NotEmpty', false, array('messages' => array( 'isEmpty' => 'Dit veld is verplicht')))
),
'value' => date('W'),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('select', 'status', array(
'label' => 'Status',
'decorators' => $this->elementDecorator,
'required' => true,
'multiOptions' => array(
'0' => 'Onbetaald',
'1' => 'Betaald',
),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('text', 'week', array(
'label' => 'Weeknummer',
'maxlength' => 5,
'size' => 5,
'decorators' => $this->elementDecorator,
'required' => true,
'validators' => array(
array('NotEmpty', false, array('messages' => array( 'isEmpty' => 'Dit veld is verplicht')))
),
'value' => date('W'),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('select', 'company_id', array(
'label' => 'Bedrijf',
'multiOptions' => array(0=>'',1=>'Bedrijf A',2=>'Bedrijf B'),
'decorators' => $this->elementDecorator,
'description' => '',
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('select', 'rate', array(
'label' => 'Tarief',
'multiOptions' => Model_Rate::getRatesSelect() ,
'decorators' => $this->elementDecorator,
'description' => '',
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('select', 'road_pricing', array(
'label' => 'Reistarief',
'multiOptions' => array('0.19'=>'€0,19','0.30'=>'€0,30') ,
'decorators' => $this->elementDecorator,
'description' => '',
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('textarea', 'description', array(
'label' => 'omschrijving',
'class' => 'newLine',
'rows' => 10,
'cols' => 25,
'decorators' => $this->elementDecoratorNewLine,
'description' => 'korte omschrijving van factuur. Wordt gebruikt in overzichten',
'required' => true,
'validators' => array(
array('NotEmpty', false, array('messages' => array( 'isEmpty' => 'Dit veld is verplicht')))
),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('button', 'submit', array(
'label' => 'Opslaan',
'class'=> 'button',
'ignore' => true,
'type' => 'submit',
'decorators' => $this->buttonDecorator,
));
$this->addSubForm(new Zend_Form_SubForm,'lines');
$this->addSubForm(new Zend_Form_SubForm,'new');
$this->addElement('button', 'submit', array(
'label' => 'Opslaan',
'class'=> 'button',
'ignore' => true,
'type' => 'submit',
'order'=>1002,
'decorators' => $this->buttonDecorator,
));
}
public function isValid($data){
$subForm = $this->getSubForm('new');
if(isset($data['new'])){
$data['new']= array();
}
foreach($data as $idx => $line){
$prefixIdx = $idx.'__';
$newSubForm = new Money_Form_SubInvoiceLine(array('prefix'=>$prefixIdx));
$subForm->addSubForm($newSubForm, $idx);
}
return parent::setDefaults($data);
}
public function setDefaults($defaults)
{
$subForm = $this->getSubForm('lines');
$subForm->clearSubForms();
foreach($defaults['lines'] as $idx => $line){
if($idx == 0) $idx =1;
$prefixIdx = $idx.'__';
$subFormLine = new Money_Form_SubInvoiceLine(array('prefix'=>$prefixIdx));
foreach($line as $name => $value){
$subFormLine->setDefault($prefixIdx.$name, $value);
}
$subForm->addSubForm($subFormLine,(string) $idx );
unset($defaults[$idx]);
}
unset($defaults['lines']);
return parent::setDefaults($defaults);
}
public function getValues(){
$values = parent::getValues(true);
$lines = array();
foreach($values['lines'] as $idx => $line ){
if($idx == 0) $idx =1;
foreach($line as $name => $value){
$key = preg_replace('/[0-9]__/','',$name);
$lines[$idx][$key] = $value;
}
}
$values['lines'] = $lines;
return $values;
}
}
[/code]
[code]
[code]<?php
Class Money_Form_SubInvoiceLine extends Form_SubBase
{
protected $_prefix = '';
public function setPrefix($prefix = null){
$this->_prefix = $prefix;
}
public function init(){
$this->addElement('hidden', $this->_prefix.'invoice_id', array(
'required' => false,
'decorators' => $this->hiddenDecorator
));
$this->addElement('hidden', $this->_prefix.'invoice_line_id', array(
'required' => false,
'decorators' => $this->hiddenDecorator
));
$this->addElement('text', $this->_prefix.'description', array(
'label' => 'omschrijving',
'maxlength' => 250,
'size' => 10,
'decorators' => $this->elementDecorator,
'description' => 'korte omschrijving',
'required' => true,
'validators' => array(
array('NotEmpty', false, array('messages' => array( 'isEmpty' => 'Dit veld is verplicht')))
),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('text', $this->_prefix.'hours', array(
'label' => 'Uren',
'maxlength' => 100,
'size' => 4,
'decorators' => $this->elementDecorator,
'required' => true,
'validators' => array(
array('NotEmpty', false, array('messages' => array( 'isEmpty' => 'Dit veld is verplicht')))
),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('text', $this->_prefix.'travel_klm', array(
'label' => 'Kilometers',
'maxlength' => 100,
'size' => 4,
'decorators' => $this->elementDecorator,
'required' => false,
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('text', $this->_prefix.'price_klm', array(
'label' => 'Klm prijs',
'maxlength' => 100,
'size' => 4,
'decorators' => $this->elementDecorator,
'required' => false,
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('select', $this->_prefix.'rate', array(
'label' => 'Tarief',
'multiOptions' => Model_Rate::getRatesSelect() ,
'decorators' => $this->elementDecorator,
'description' => '',
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('select', $this->_prefix.'cost_percentage', array(
'label' => 'Toeslag',
'multiOptions' => array('100'=>'Geen','110'=>'110%','150'=>'150%','200'=>'200%'),
'decorators' => $this->elementDecorator,
'description' => '',
'filters' => array('stringTrim', 'stripTags'),
));
}
}
[/code]
class Money_Form_Invoice extends Form_Base
{
public function init()
{
$this->setMethod('post')
->setName('invoice')
->setDecorators($this->formDecorators);
$this->addElement('hidden', 'invoice_id', array(
'required' => false,
'decorators' => $this->hiddenDecorator
));
$this->addElement('hidden', 'year', array(
'required' => false,
'decorators' => $this->hiddenDecorator
));
$this->addElement('hidden','serial_code',array(
'required' => true ,
'decorators'=> $this->hiddenDecorator,
));
$this->addElement('text', 'ordernumber', array(
'label' => 'Werkorder',
'maxlength' => 45,
'size' => 15,
'decorators' => $this->elementDecorator,
'required' => true,
'validators' => array(
array('NotEmpty', false, array('messages' => array( 'isEmpty' => 'Dit veld is verplicht')))
),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('text', 'number', array(
'label' => 'Factuur nummer',
'maxlength' => 15,
'size' => 15,
'decorators' => $this->elementDecorator,
'required' => true,
'validators' => array(
array('NotEmpty', false, array('messages' => array( 'isEmpty' => 'Dit veld is verplicht')))
),
'value' => date('W'),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('select', 'status', array(
'label' => 'Status',
'decorators' => $this->elementDecorator,
'required' => true,
'multiOptions' => array(
'0' => 'Onbetaald',
'1' => 'Betaald',
),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('text', 'week', array(
'label' => 'Weeknummer',
'maxlength' => 5,
'size' => 5,
'decorators' => $this->elementDecorator,
'required' => true,
'validators' => array(
array('NotEmpty', false, array('messages' => array( 'isEmpty' => 'Dit veld is verplicht')))
),
'value' => date('W'),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('select', 'company_id', array(
'label' => 'Bedrijf',
'multiOptions' => array(0=>'',1=>'Bedrijf A',2=>'Bedrijf B'),
'decorators' => $this->elementDecorator,
'description' => '',
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('select', 'rate', array(
'label' => 'Tarief',
'multiOptions' => Model_Rate::getRatesSelect() ,
'decorators' => $this->elementDecorator,
'description' => '',
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('select', 'road_pricing', array(
'label' => 'Reistarief',
'multiOptions' => array('0.19'=>'€0,19','0.30'=>'€0,30') ,
'decorators' => $this->elementDecorator,
'description' => '',
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('textarea', 'description', array(
'label' => 'omschrijving',
'class' => 'newLine',
'rows' => 10,
'cols' => 25,
'decorators' => $this->elementDecoratorNewLine,
'description' => 'korte omschrijving van factuur. Wordt gebruikt in overzichten',
'required' => true,
'validators' => array(
array('NotEmpty', false, array('messages' => array( 'isEmpty' => 'Dit veld is verplicht')))
),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('button', 'submit', array(
'label' => 'Opslaan',
'class'=> 'button',
'ignore' => true,
'type' => 'submit',
'decorators' => $this->buttonDecorator,
));
$this->addSubForm(new Zend_Form_SubForm,'lines');
$this->addSubForm(new Zend_Form_SubForm,'new');
$this->addElement('button', 'submit', array(
'label' => 'Opslaan',
'class'=> 'button',
'ignore' => true,
'type' => 'submit',
'order'=>1002,
'decorators' => $this->buttonDecorator,
));
}
public function isValid($data){
$subForm = $this->getSubForm('new');
if(isset($data['new'])){
$data['new']= array();
}
foreach($data as $idx => $line){
$prefixIdx = $idx.'__';
$newSubForm = new Money_Form_SubInvoiceLine(array('prefix'=>$prefixIdx));
$subForm->addSubForm($newSubForm, $idx);
}
return parent::setDefaults($data);
}
public function setDefaults($defaults)
{
$subForm = $this->getSubForm('lines');
$subForm->clearSubForms();
foreach($defaults['lines'] as $idx => $line){
if($idx == 0) $idx =1;
$prefixIdx = $idx.'__';
$subFormLine = new Money_Form_SubInvoiceLine(array('prefix'=>$prefixIdx));
foreach($line as $name => $value){
$subFormLine->setDefault($prefixIdx.$name, $value);
}
$subForm->addSubForm($subFormLine,(string) $idx );
unset($defaults[$idx]);
}
unset($defaults['lines']);
return parent::setDefaults($defaults);
}
public function getValues(){
$values = parent::getValues(true);
$lines = array();
foreach($values['lines'] as $idx => $line ){
if($idx == 0) $idx =1;
foreach($line as $name => $value){
$key = preg_replace('/[0-9]__/','',$name);
$lines[$idx][$key] = $value;
}
}
$values['lines'] = $lines;
return $values;
}
}
[/code]
[code]
[code]<?php
Class Money_Form_SubInvoiceLine extends Form_SubBase
{
protected $_prefix = '';
public function setPrefix($prefix = null){
$this->_prefix = $prefix;
}
public function init(){
$this->addElement('hidden', $this->_prefix.'invoice_id', array(
'required' => false,
'decorators' => $this->hiddenDecorator
));
$this->addElement('hidden', $this->_prefix.'invoice_line_id', array(
'required' => false,
'decorators' => $this->hiddenDecorator
));
$this->addElement('text', $this->_prefix.'description', array(
'label' => 'omschrijving',
'maxlength' => 250,
'size' => 10,
'decorators' => $this->elementDecorator,
'description' => 'korte omschrijving',
'required' => true,
'validators' => array(
array('NotEmpty', false, array('messages' => array( 'isEmpty' => 'Dit veld is verplicht')))
),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('text', $this->_prefix.'hours', array(
'label' => 'Uren',
'maxlength' => 100,
'size' => 4,
'decorators' => $this->elementDecorator,
'required' => true,
'validators' => array(
array('NotEmpty', false, array('messages' => array( 'isEmpty' => 'Dit veld is verplicht')))
),
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('text', $this->_prefix.'travel_klm', array(
'label' => 'Kilometers',
'maxlength' => 100,
'size' => 4,
'decorators' => $this->elementDecorator,
'required' => false,
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('text', $this->_prefix.'price_klm', array(
'label' => 'Klm prijs',
'maxlength' => 100,
'size' => 4,
'decorators' => $this->elementDecorator,
'required' => false,
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('select', $this->_prefix.'rate', array(
'label' => 'Tarief',
'multiOptions' => Model_Rate::getRatesSelect() ,
'decorators' => $this->elementDecorator,
'description' => '',
'filters' => array('stringTrim', 'stripTags'),
));
$this->addElement('select', $this->_prefix.'cost_percentage', array(
'label' => 'Toeslag',
'multiOptions' => array('100'=>'Geen','110'=>'110%','150'=>'150%','200'=>'200%'),
'decorators' => $this->elementDecorator,
'description' => '',
'filters' => array('stringTrim', 'stripTags'),
));
}
}
[/code]