Skip to content

Commit f2bf542

Browse files
committed
Update docs
1 parent c8ed675 commit f2bf542

File tree

3 files changed

+155
-57
lines changed

3 files changed

+155
-57
lines changed

README.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ any feedback, corrections additions you can provide.
1111
Installation
1212
------------
1313

14-
Installation instructions [can be found here][].
14+
Installation instructions [can be found here](Resources/doc/installation.md).
1515

1616
PolyCollection
1717
--------------
@@ -27,7 +27,7 @@ like `InvoiceProductLine`, `InvoiceShippingLine` and `InvoiceDiscountLine`
2727
you could use this form type to achieve a form collection that would support
2828
all 4 types of `InvoiceLine` inside the same collection.
2929

30-
For more information see the [PolyCollection Documentation][].
30+
For more information see the [PolyCollection Documentation](Resources/doc/polycollection.md).
3131

3232
Collection Helper
3333
-----------------
@@ -36,7 +36,7 @@ InfiniteFormBundle supplies some helper javascript for working with form collect
3636
supports both the standard Symfony2 collection type and the PolyCollection type supplied
3737
by this bundle.
3838

39-
For more information see the [Collection Helper Documentation][].
39+
For more information see the [Collection Helper Documentation](Resources/doc/collection-helper.md).
4040

4141
CheckboxGrid
4242
------------
@@ -50,18 +50,12 @@ different areas. Any of its salesmen could sell any combination of products
5050
in areas. The salesman form needs a table of checkboxes where the rows are
5151
products and the columns are areas (or vice versa!)
5252

53-
For more information see the [CheckboxGrid Documentation][].
53+
For more information see the [CheckboxGrid Documentation](Resources/doc/checkboxgrid.md).
5454

5555
Twig Helper
5656
-----------
5757

5858
InfiniteFormBundle comes with a Twig extension that adds form specific helpers
5959
for use when rendering templates.
6060

61-
For more information see the [Twig Helper][].
62-
63-
[PolyCollection Documentation]: https://github.com/infinite-networks/InfiniteFormBundle/blob/master/Resources/doc/polycollection.md
64-
[Collection Helper Documentation]: https://github.com/infinite-networks/InfiniteFormBundle/blob/master/Resources/doc/collection-helper.md
65-
[CheckboxGrid Documentation]: https://github.com/infinite-networks/InfiniteFormBundle/blob/master/Resources/doc/checkboxgrid.md
66-
[Twig Helper]: https://github.com/infinite-networks/InfiniteFormBundle/blob/master/Resources/doc/twig-helper.md
67-
[can be found here]: https://github.com/infinite-networks/InfiniteFormBundle/blob/master/Resources/doc/installation.md
61+
For more information see the [Twig Helper](Resources/doc/twig-helper.md).

Resources/doc/checkboxgrid.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Synopsis
77
This:
88

99
```php
10-
$builder->add('productAreas', 'infinite_form_entity_checkbox_grid', array(
10+
$builder->add('productAreas', EntityCheckboxGridType::class, array(
1111
'class' => 'Acme\DemoBundle\Entity\SalesmanProductArea',
1212
'x_path' => 'productSold',
1313
'y_path' => 'areaServiced',
@@ -16,7 +16,7 @@ This:
1616

1717
Becomes this:
1818

19-
![Rendered checkbox grid](https://raw.github.com/infinite-networks/InfiniteFormBundle/master/Resources/doc/checkboxgrid.png)
19+
![Rendered checkbox grid](checkboxgrid.png)
2020

2121
Introduction
2222
------------
@@ -56,28 +56,30 @@ A very simple example:
5656

5757
namespace Acme\DemoBundle\Form;
5858

59+
use Infinite\FormBundle\Form\Type\EntityCheckboxGridType;
5960
use Symfony\Component\Form\AbstractType;
6061
use Symfony\Component\Form\FormBuilderInterface;
61-
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
62+
use Symfony\Component\Form\Extension\Core\Type\TextType;
63+
use Symfony\Component\OptionsResolver\OptionsResolver;
6264

6365
class SalesmanType extends AbstractType
6466
{
6567
public function buildForm(FormBuilderInterface $builder, array $options)
6668
{
67-
$builder->add('name', 'text');
68-
$builder->add('productAreas', 'infinite_form_entity_checkbox_grid', array(
69+
$builder->add('name', TextType::class);
70+
$builder->add('productAreas', EntityCheckboxGrid::class, array(
6971
'class' => 'Acme\DemoBundle\Entity\SalesmanProductArea',
7072
'x_path' => 'productSold',
7173
'y_path' => 'areaServiced',
7274
));
7375
}
7476

75-
public function getName()
77+
public function getBlockPrefix()
7678
{
7779
return 'salesman';
7880
}
7981

80-
public function setDefaultOptions(OptionsResolverInterface $resolver)
82+
public function configureOptions(OptionsResolver $resolver)
8183
{
8284
$resolver->setDefaults(array(
8385
'data_class' => 'Acme\DemoBundle\Entity\Salesman',
@@ -101,7 +103,7 @@ Finally, you can exclude checkboxes with a cell_filter closure.
101103
An example with more options:
102104

103105
```php
104-
$builder->add('productAreas', 'infinite_form_entity_checkbox_grid', array(
106+
$builder->add('productAreas', EntityCheckboxGridType::class, array(
105107
'class' => 'Acme\DemoBundle\Entity\SalesmanProductArea',
106108

107109
'x_path' => 'productSold',
@@ -113,7 +115,7 @@ An example with more options:
113115

114116
'y_path' => 'areaServiced',
115117
'y_label_path' => 'name',
116-
'y_choice_list' => $areaChoiceList, // An EntityChoiceList constructed elsewhere
118+
'y_choices' => $areaChoices, // An array of options built elsewhere
117119

118120
'cell_filter' => function ($x, $y) {
119121
// We cannot sell tables in the north due to contractual obligations
@@ -130,8 +132,8 @@ CSS styles or you'd like to add some Javascript to allow checking every box
130132
in a row or column at once.
131133

132134
Open your form theme and add blocks for infinite_form_checkbox_grid_widget
133-
and infinite_form_checkbox_row_widget. Use our [default form theme](https://github.com/infinite-networks/InfiniteFormBundle/blob/master/Resources/views/form_theme.html.twig)
135+
and infinite_form_checkbox_row_widget. Use our [default form theme](../views/form_theme.html.twig)
134136
as a guide for what to put there.
135137

136-
See [Form Theming in Twig](http://symfony.com/doc/2.2/cookbook/form/form_customization.html#form-theming-in-twig)
138+
See [Form Theming in Twig](http://symfony.com/doc/3.1/cookbook/form/form_customization.html#form-theming-in-twig)
137139
if you're not sure where to add the blocks.

Resources/doc/polycollection.md

Lines changed: 137 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -117,35 +117,38 @@ PolyCollection to know which type to use when it encounters an object.
117117

118118
namespace Infinite\InvoiceBundle\Form\Type;
119119

120-
use Symfony\Component\Form\AbstractType as BaseType;
120+
use Infinite\FormBundle\Form\Type\PolyCollectionType;
121+
use Infinite\InvoiceBundle\Entity\Invoice;
122+
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
123+
use Symfony\Component\Form\AbstractType;
121124
use Symfony\Component\Form\FormBuilderInterface;
122-
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
125+
use Symfony\Component\OptionsResolver\OptionsResolver;
123126

124-
class InvoiceType extends BaseType
127+
class InvoiceType extends AbstractType
125128
{
126129
public function buildForm(FormBuilderInterface $builder, array $options)
127130
{
128-
$builder->add('customer', 'entity', array( /* ... */ ));
129-
$builder->add('address', 'entity', array( /* ... */ ));
131+
$builder->add('customer', EntityType::class, array( /* ... */ ));
132+
$builder->add('address', EntityType::class, array( /* ... */ ));
130133

131-
$builder->add('lines', 'infinite_form_polycollection', array(
134+
$builder->add('lines', PolycollectionType::class, array(
132135
'types' => array(
133-
'invoice_line_type', // The first defined Type becomes the default
134-
'invoice_product_line_type',
136+
InvoiceLineType::class,
137+
InvoiceProductLineType::class,
135138
),
136139
'allow_add' => true,
137140
'allow_delete' => true,
138141
));
139142
}
140143

141-
public function setDefaultOptions(OptionsResolverInterface $resolver)
144+
public function configureOptions(OptionsResolver $resolver)
142145
{
143-
$resolver->setDefaults(array('data_class' => 'Infinite\\InvoiceBundle\\Entity\\Invoice'));
146+
$resolver->setDefaults(array('data_class' => Invoice::class));
144147
}
145148

146-
public function getName()
149+
public function getBlockPrefix()
147150
{
148-
return 'invoice_type';
151+
return 'invoice';
149152
}
150153
}
151154
```
@@ -156,37 +159,40 @@ class InvoiceType extends BaseType
156159

157160
namespace Infinite\InvoiceBundle\Form\Type;
158161

159-
use Symfony\Component\Form\AbstractType as BaseType;
162+
use Infinite\InvoiceBundle\Entity\InvoiceLine;
163+
use Symfony\Component\Form\AbstractType;
164+
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
165+
use Symfony\Component\Form\Extension\Core\Type\NumberType;
166+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
167+
use Symfony\Component\Form\Extension\Core\Type\TextType;
160168
use Symfony\Component\Form\FormBuilderInterface;
161-
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
169+
use Symfony\Component\OptionsResolver\OptionsResolver;
162170

163-
class InvoiceLineType extends BaseType
171+
class InvoiceLineType extends AbstractType
164172
{
165-
protected $dataClass = 'Infinite\\InvoiceBundle\\Entity\\InvoiceLine';
166-
167173
public function buildForm(FormBuilderInterface $builder, array $options)
168174
{
169-
$builder->add('quantity', 'number');
170-
$builder->add('unitAmount', 'text');
171-
$builder->add('description', 'textarea');
175+
$builder->add('quantity', NumberType::class);
176+
$builder->add('unitAmount', TextType::class);
177+
$builder->add('description', TextareaType::class);
172178

173-
$builder->add('_type', 'hidden', array(
174-
'data' => $this->getName(),
179+
$builder->add('_type', HiddenType::class, array(
180+
'data' => 'line', // Arbitrary, but must be distinct
175181
'mapped' => false
176182
));
177183
}
178184

179-
public function setDefaultOptions(OptionsResolverInterface $resolver)
185+
public function configureOptions(OptionsResolver $resolver)
180186
{
181187
$resolver->setDefaults(array(
182-
'data_class' => $this->dataClass,
183-
'model_class' => $this->dataClass,
188+
'data_class' => InvoiceLine::class,
189+
'model_class' => InvoiceLine::class,
184190
));
185191
}
186192

187-
public function getName()
193+
public function getBlockPrefix()
188194
{
189-
return 'invoice_line_type';
195+
return 'invoice_line';
190196
}
191197
}
192198
```
@@ -197,29 +203,125 @@ class InvoiceLineType extends BaseType
197203

198204
namespace Infinite\InvoiceBundle\Form\Type;
199205

206+
use Infinite\InvoiceBundle\Entity\InvoiceProductLine;
207+
use Symfony\Component\Form\AbstractType;
208+
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
209+
use Symfony\Component\Form\Extension\Core\Type\NumberType;
210+
use Symfony\Component\Form\Extension\Core\Type\TextType;
200211
use Symfony\Component\Form\FormBuilderInterface;
212+
use Symfony\Component\OptionsResolver\OptionsResolver;
201213

202-
class InvoiceProductType extends InvoiceLineType
214+
class InvoiceProductType extends AbstractType
203215
{
204-
protected $dataClass = 'Infinite\\InvoiceBundle\\Entity\\InvoiceProductType';
205-
206216
public function buildForm(FormBuilderInterface $builder, array $options)
207217
{
208-
parent::buildForm($builder, $options);
218+
$builder->add('quantity', NumberType::class);
209219

210-
$builder->add('product', 'entity', array(
220+
$builder->add('product', EntityType::class, array(
211221
// entity field definition here
212222
));
223+
224+
$builder->add('_type', HiddenType::class, array(
225+
'data' => 'product', // Arbitrary, but must be distinct
226+
'mapped' => false
227+
));
213228
}
214229

215-
public function getName()
230+
public function configureOptions(OptionsResolver $resolver)
216231
{
217-
return 'invoice_product_line_type';
232+
$resolver->setDefaults(array(
233+
'data_class' => InvoiceProductLine::class,
234+
'model_class' => InvoiceProductLine::class,
235+
));
236+
}
237+
238+
public function getBlockPrefix()
239+
{
240+
return 'invoice_product_line';
218241
}
219242
}
220243
```
221244

222245
Rendering the form
223246
------------------
224247

225-
Coming Soon. Still a work in progress.
248+
Polycollections require manual work to render. This code can go
249+
in the same template that renders the rest of the form.
250+
251+
You will need to render add buttons from the prototypes array, which is
252+
keyed on the _type field in the form definition.
253+
254+
It is best illustrated by example.
255+
256+
```twig
257+
{# AppBundle:Invoice:add.html.twig #}
258+
259+
{% form_theme form.lines _self %}
260+
261+
{# ... #}
262+
263+
{% block infinite_form_polycollection_row %}
264+
{% set collectionForm = form %}
265+
<hr>
266+
<div class="collection">
267+
<div class="clearfix">
268+
<div class="pull-left">
269+
{{ form_label(collectionForm, 'Invoice lines') }}
270+
</div>
271+
<div class="pull-right">
272+
{% set form = prototypes.line %}
273+
<a href="#" data-prototype="{{ block('entry_row') | escape }}"
274+
class="btn btn-success add_item">
275+
<i class="glyphicon glyphicon-plus"></i> Freight line
276+
</a>
277+
{% set form = prototypes.product %}
278+
<a href="#" data-prototype="{{ block('entry_row') | escape }}"
279+
class="btn btn-success add_item">
280+
<i class="glyphicon glyphicon-plus"></i>
281+
</a>
282+
</div>
283+
</div>
284+
<div class="items">
285+
{% for form in collectionForm %}
286+
{{ block('entry_row') }}
287+
{% endfor %}
288+
</div>
289+
</div>
290+
{% endblock %}
291+
292+
{% block entry_row %}
293+
<div class="item">
294+
<hr>
295+
{{ form_widget(form) }}
296+
</div>
297+
{% endblock %}
298+
299+
{% block invoice_line_widget %}
300+
<div class="row">
301+
<div class="col-md-6">{{ form_row(form.description) }}</div>
302+
<div class="col-md-2">{{ form_row(form.unitAmount) }}</div>
303+
<div class="col-md-2">{{ form_row(form.quantity) }}</div>
304+
<div class="col-md-2 text-right">
305+
<label>&nbsp;</label><br>
306+
<a href="#" class="btn btn-danger remove_item">
307+
<i class="glyphicon glyphicon-minus"></i> Remove
308+
</a>
309+
</div>
310+
</div>
311+
{{ form_rest(form) }}
312+
{% endblock %}
313+
314+
{% block invoice_product_line_widget %}
315+
<div class="row">
316+
<div class="col-md-6">{{ form_row(form.product) }}</div>
317+
<div class="col-md-2 col-md-offset-2">{{ form_row(form.quantity) }}</div>
318+
<div class="col-md-2 text-right">
319+
<label>&nbsp;</label><br>
320+
<a href="#" class="btn btn-danger remove_item">
321+
<i class="glyphicon glyphicon-minus"></i> Remove
322+
</a>
323+
</div>
324+
</div>
325+
{{ form_rest(form) }}
326+
{% endblock %}
327+
```

0 commit comments

Comments
 (0)