Skip to content

Commit aec1016

Browse files
committed
Added PHP types to the Form related articles
1 parent 90c83b2 commit aec1016

19 files changed

+224
-197
lines changed

form/create_custom_field_type.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ By convention they are stored in the ``src/Form/Type/`` directory::
3838

3939
class ShippingType extends AbstractType
4040
{
41-
public function configureOptions(OptionsResolver $resolver)
41+
public function configureOptions(OptionsResolver $resolver): void
4242
{
4343
$resolver->setDefaults([
4444
'choices' => [
@@ -49,7 +49,7 @@ By convention they are stored in the ``src/Form/Type/`` directory::
4949
]);
5050
}
5151

52-
public function getParent()
52+
public function getParent(): string
5353
{
5454
return ChoiceType::class;
5555
}
@@ -82,7 +82,7 @@ Now you can add this form type when :doc:`creating Symfony forms </forms>`::
8282

8383
class OrderType extends AbstractType
8484
{
85-
public function buildForm(FormBuilderInterface $builder, array $options)
85+
public function buildForm(FormBuilderInterface $builder, array $options): void
8686
{
8787
$builder
8888
// ...
@@ -168,7 +168,7 @@ in the postal address. For the moment, all fields are of type ``TextType``::
168168
{
169169
// ...
170170

171-
public function buildForm(FormBuilderInterface $builder, array $options)
171+
public function buildForm(FormBuilderInterface $builder, array $options): void
172172
{
173173
$builder
174174
->add('addressLine1', TextType::class, [
@@ -209,7 +209,7 @@ correctly rendered in any template::
209209

210210
class OrderType extends AbstractType
211211
{
212-
public function buildForm(FormBuilderInterface $builder, array $options)
212+
public function buildForm(FormBuilderInterface $builder, array $options): void
213213
{
214214
$builder
215215
// ...
@@ -254,7 +254,7 @@ to define, validate and process their values::
254254
{
255255
// ...
256256

257-
public function configureOptions(OptionsResolver $resolver)
257+
public function configureOptions(OptionsResolver $resolver): void
258258
{
259259
// this defines the available options and their default values when
260260
// they are not configured explicitly when using the form type
@@ -293,7 +293,7 @@ Now you can configure these options when using the form type::
293293

294294
class OrderType extends AbstractType
295295
{
296-
public function buildForm(FormBuilderInterface $builder, array $options)
296+
public function buildForm(FormBuilderInterface $builder, array $options): void
297297
{
298298
$builder
299299
// ...
@@ -320,7 +320,7 @@ The last step is to use these options when building the form::
320320
{
321321
// ...
322322

323-
public function buildForm(FormBuilderInterface $builder, array $options)
323+
public function buildForm(FormBuilderInterface $builder, array $options): void
324324
{
325325
// ...
326326

@@ -473,7 +473,7 @@ defined by the form or be completely independent::
473473

474474
// ...
475475

476-
public function buildView(FormView $view, FormInterface $form, array $options)
476+
public function buildView(FormView $view, FormInterface $form, array $options): void
477477
{
478478
// pass the form type option directly to the template
479479
$view->vars['isExtendedAddress'] = $options['is_extended_address'];

form/create_form_type_extension.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ the database::
114114

115115
// ...
116116

117-
public function getWebPath()
117+
public function getWebPath(): string
118118
{
119119
// ... $webPath being the full image URL, to be used in templates
120120

@@ -149,13 +149,13 @@ For example::
149149
return [FileType::class];
150150
}
151151

152-
public function configureOptions(OptionsResolver $resolver)
152+
public function configureOptions(OptionsResolver $resolver): void
153153
{
154154
// makes it legal for FileType fields to have an image_property option
155155
$resolver->setDefined(['image_property']);
156156
}
157157

158-
public function buildView(FormView $view, FormInterface $form, array $options)
158+
public function buildView(FormView $view, FormInterface $form, array $options): void
159159
{
160160
if (isset($options['image_property'])) {
161161
// this will be whatever class/entity is bound to your form (e.g. Media)
@@ -221,7 +221,7 @@ next to the file field. For example::
221221

222222
class MediaType extends AbstractType
223223
{
224-
public function buildForm(FormBuilderInterface $builder, array $options)
224+
public function buildForm(FormBuilderInterface $builder, array $options): void
225225
{
226226
$builder
227227
->add('name', TextType::class)

form/data_based_validation.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ to an array callback::
1212
use Symfony\Component\OptionsResolver\OptionsResolver;
1313

1414
// ...
15-
public function configureOptions(OptionsResolver $resolver)
15+
public function configureOptions(OptionsResolver $resolver): void
1616
{
1717
$resolver->setDefaults([
1818
'validation_groups' => [
@@ -32,7 +32,7 @@ example). You can also define whole logic inline by using a ``Closure``::
3232
use Symfony\Component\OptionsResolver\OptionsResolver;
3333

3434
// ...
35-
public function configureOptions(OptionsResolver $resolver)
35+
public function configureOptions(OptionsResolver $resolver): void
3636
{
3737
$resolver->setDefaults([
3838
'validation_groups' => function (FormInterface $form) {
@@ -56,7 +56,7 @@ of the entity as well you have to adjust the option as follows::
5656
use Symfony\Component\OptionsResolver\OptionsResolver;
5757

5858
// ...
59-
public function configureOptions(OptionsResolver $resolver)
59+
public function configureOptions(OptionsResolver $resolver): void
6060
{
6161
$resolver->setDefaults([
6262
'validation_groups' => function (FormInterface $form) {

form/data_mappers.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ in your form type::
9898
/**
9999
* @param Color|null $viewData
100100
*/
101-
public function mapDataToForms($viewData, $forms)
101+
public function mapDataToForms($viewData, $forms): void
102102
{
103103
// there is no data yet, so nothing to prepopulate
104104
if (null === $viewData) {
@@ -119,7 +119,7 @@ in your form type::
119119
$forms['blue']->setData($viewData->getBlue());
120120
}
121121

122-
public function mapFormsToData($forms, &$viewData)
122+
public function mapFormsToData($forms, &$viewData): void
123123
{
124124
/** @var FormInterface[] $forms */
125125
$forms = iterator_to_array($forms);
@@ -158,7 +158,7 @@ method::
158158

159159
final class ColorType extends AbstractType implements DataMapperInterface
160160
{
161-
public function buildForm(FormBuilderInterface $builder, array $options)
161+
public function buildForm(FormBuilderInterface $builder, array $options): void
162162
{
163163
$builder
164164
->add('red', IntegerType::class, [
@@ -177,7 +177,7 @@ method::
177177
;
178178
}
179179

180-
public function configureOptions(OptionsResolver $resolver)
180+
public function configureOptions(OptionsResolver $resolver): void
181181
{
182182
// when creating a new color, the initial data should be null
183183
$resolver->setDefault('empty_data', null);

form/data_transformers.rst

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ Suppose you have a Task form with a tags ``text`` type::
4040
// ...
4141
class TaskType extends AbstractType
4242
{
43-
public function buildForm(FormBuilderInterface $builder, array $options)
43+
public function buildForm(FormBuilderInterface $builder, array $options): void
4444
{
4545
$builder->add('tags', TextType::class);
4646
}
4747

48-
public function configureOptions(OptionsResolver $resolver)
48+
public function configureOptions(OptionsResolver $resolver): void
4949
{
5050
$resolver->setDefaults([
5151
'data_class' => Task::class,
@@ -72,7 +72,7 @@ class::
7272

7373
class TaskType extends AbstractType
7474
{
75-
public function buildForm(FormBuilderInterface $builder, array $options)
75+
public function buildForm(FormBuilderInterface $builder, array $options): void
7676
{
7777
$builder->add('tags', TextType::class);
7878

@@ -136,15 +136,15 @@ Start by setting up the text field like normal::
136136
// ...
137137
class TaskType extends AbstractType
138138
{
139-
public function buildForm(FormBuilderInterface $builder, array $options)
139+
public function buildForm(FormBuilderInterface $builder, array $options): void
140140
{
141141
$builder
142142
->add('description', TextareaType::class)
143143
->add('issue', TextType::class)
144144
;
145145
}
146146

147-
public function configureOptions(OptionsResolver $resolver)
147+
public function configureOptions(OptionsResolver $resolver): void
148148
{
149149
$resolver->setDefaults([
150150
'data_class' => Task::class,
@@ -188,9 +188,8 @@ to and from the issue number and the ``Issue`` object::
188188
* Transforms an object (issue) to a string (number).
189189
*
190190
* @param Issue|null $issue
191-
* @return string
192191
*/
193-
public function transform($issue)
192+
public function transform($issue): string
194193
{
195194
if (null === $issue) {
196195
return '';
@@ -203,10 +202,9 @@ to and from the issue number and the ``Issue`` object::
203202
* Transforms a string (number) to an object (issue).
204203
*
205204
* @param string $issueNumber
206-
* @return Issue|null
207205
* @throws TransformationFailedException if object (issue) is not found.
208206
*/
209-
public function reverseTransform($issueNumber)
207+
public function reverseTransform($issueNumber): ?Issue
210208
{
211209
// no issue number? It's optional, so that's ok
212210
if (!$issueNumber) {
@@ -273,7 +271,7 @@ and type-hint the new class::
273271
$this->transformer = $transformer;
274272
}
275273

276-
public function buildForm(FormBuilderInterface $builder, array $options)
274+
public function buildForm(FormBuilderInterface $builder, array $options): void
277275
{
278276
$builder
279277
->add('description', TextareaType::class)
@@ -306,7 +304,7 @@ end-user error message in the data transformer using the
306304
{
307305
// ...
308306

309-
public function reverseTransform($issueNumber)
307+
public function reverseTransform($issueNumber): ?Issue
310308
{
311309
// ...
312310

@@ -394,19 +392,19 @@ First, create the custom field type class::
394392
$this->transformer = $transformer;
395393
}
396394

397-
public function buildForm(FormBuilderInterface $builder, array $options)
395+
public function buildForm(FormBuilderInterface $builder, array $options): void
398396
{
399397
$builder->addModelTransformer($this->transformer);
400398
}
401399

402-
public function configureOptions(OptionsResolver $resolver)
400+
public function configureOptions(OptionsResolver $resolver): void
403401
{
404402
$resolver->setDefaults([
405403
'invalid_message' => 'The selected issue does not exist',
406404
]);
407405
}
408406

409-
public function getParent()
407+
public function getParent(): string
410408
{
411409
return TextType::class;
412410
}
@@ -427,7 +425,7 @@ As long as you're using :ref:`autowire <services-autowire>` and
427425

428426
class TaskType extends AbstractType
429427
{
430-
public function buildForm(FormBuilderInterface $builder, array $options)
428+
public function buildForm(FormBuilderInterface $builder, array $options): void
431429
{
432430
$builder
433431
->add('description', TextareaType::class)

form/direct_submit.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ to detect when the form has been submitted. However, you can also use the
1111
control over when exactly your form is submitted and what data is passed to it::
1212

1313
use Symfony\Component\HttpFoundation\Request;
14+
use Symfony\Component\HttpFoundation\Response;
1415
// ...
1516

16-
public function new(Request $request)
17+
public function new(Request $request): Response
1718
{
1819
$task = new Task();
1920
$form = $this->createForm(TaskType::class, $task);

form/disabling_validation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ these cases you can set the ``validation_groups`` option to ``false``::
99

1010
use Symfony\Component\OptionsResolver\OptionsResolver;
1111

12-
public function configureOptions(OptionsResolver $resolver)
12+
public function configureOptions(OptionsResolver $resolver): void
1313
{
1414
$resolver->setDefaults([
1515
'validation_groups' => false,

0 commit comments

Comments
 (0)