Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
Added PHP types to the Form related articles
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Nov 21, 2020
1 parent 90c83b2 commit aec1016
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 197 deletions.
18 changes: 9 additions & 9 deletions form/create_custom_field_type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ By convention they are stored in the ``src/Form/Type/`` directory::

class ShippingType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'choices' => [
Expand All @@ -49,7 +49,7 @@ By convention they are stored in the ``src/Form/Type/`` directory::
]);
}

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

class OrderType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
// ...
Expand Down Expand Up @@ -168,7 +168,7 @@ in the postal address. For the moment, all fields are of type ``TextType``::
{
// ...

public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('addressLine1', TextType::class, [
Expand Down Expand Up @@ -209,7 +209,7 @@ correctly rendered in any template::

class OrderType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
// ...
Expand Down Expand Up @@ -254,7 +254,7 @@ to define, validate and process their values::
{
// ...

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

class OrderType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
// ...
Expand All @@ -320,7 +320,7 @@ The last step is to use these options when building the form::
{
// ...

public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
// ...

Expand Down Expand Up @@ -473,7 +473,7 @@ defined by the form or be completely independent::

// ...

public function buildView(FormView $view, FormInterface $form, array $options)
public function buildView(FormView $view, FormInterface $form, array $options): void
{
// pass the form type option directly to the template
$view->vars['isExtendedAddress'] = $options['is_extended_address'];
Expand Down
8 changes: 4 additions & 4 deletions form/create_form_type_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ the database::

// ...

public function getWebPath()
public function getWebPath(): string
{
// ... $webPath being the full image URL, to be used in templates

Expand Down Expand Up @@ -149,13 +149,13 @@ For example::
return [FileType::class];
}

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

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

class MediaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', TextType::class)
Expand Down
6 changes: 3 additions & 3 deletions form/data_based_validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ to an array callback::
use Symfony\Component\OptionsResolver\OptionsResolver;

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

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

// ...
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'validation_groups' => function (FormInterface $form) {
Expand Down
8 changes: 4 additions & 4 deletions form/data_mappers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ in your form type::
/**
* @param Color|null $viewData
*/
public function mapDataToForms($viewData, $forms)
public function mapDataToForms($viewData, $forms): void
{
// there is no data yet, so nothing to prepopulate
if (null === $viewData) {
Expand All @@ -119,7 +119,7 @@ in your form type::
$forms['blue']->setData($viewData->getBlue());
}

public function mapFormsToData($forms, &$viewData)
public function mapFormsToData($forms, &$viewData): void
{
/** @var FormInterface[] $forms */
$forms = iterator_to_array($forms);
Expand Down Expand Up @@ -158,7 +158,7 @@ method::

final class ColorType extends AbstractType implements DataMapperInterface
{
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('red', IntegerType::class, [
Expand All @@ -177,7 +177,7 @@ method::
;
}

public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
// when creating a new color, the initial data should be null
$resolver->setDefault('empty_data', null);
Expand Down
28 changes: 13 additions & 15 deletions form/data_transformers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ Suppose you have a Task form with a tags ``text`` type::
// ...
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('tags', TextType::class);
}

public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Task::class,
Expand All @@ -72,7 +72,7 @@ class::

class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('tags', TextType::class);

Expand Down Expand Up @@ -136,15 +136,15 @@ Start by setting up the text field like normal::
// ...
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('description', TextareaType::class)
->add('issue', TextType::class)
;
}

public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Task::class,
Expand Down Expand Up @@ -188,9 +188,8 @@ to and from the issue number and the ``Issue`` object::
* Transforms an object (issue) to a string (number).
*
* @param Issue|null $issue
* @return string
*/
public function transform($issue)
public function transform($issue): string
{
if (null === $issue) {
return '';
Expand All @@ -203,10 +202,9 @@ to and from the issue number and the ``Issue`` object::
* Transforms a string (number) to an object (issue).
*
* @param string $issueNumber
* @return Issue|null
* @throws TransformationFailedException if object (issue) is not found.
*/
public function reverseTransform($issueNumber)
public function reverseTransform($issueNumber): ?Issue
{
// no issue number? It's optional, so that's ok
if (!$issueNumber) {
Expand Down Expand Up @@ -273,7 +271,7 @@ and type-hint the new class::
$this->transformer = $transformer;
}

public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('description', TextareaType::class)
Expand Down Expand Up @@ -306,7 +304,7 @@ end-user error message in the data transformer using the
{
// ...

public function reverseTransform($issueNumber)
public function reverseTransform($issueNumber): ?Issue
{
// ...

Expand Down Expand Up @@ -394,19 +392,19 @@ First, create the custom field type class::
$this->transformer = $transformer;
}

public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addModelTransformer($this->transformer);
}

public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'invalid_message' => 'The selected issue does not exist',
]);
}

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

class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('description', TextareaType::class)
Expand Down
3 changes: 2 additions & 1 deletion form/direct_submit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ to detect when the form has been submitted. However, you can also use the
control over when exactly your form is submitted and what data is passed to it::

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
// ...

public function new(Request $request)
public function new(Request $request): Response
{
$task = new Task();
$form = $this->createForm(TaskType::class, $task);
Expand Down
2 changes: 1 addition & 1 deletion form/disabling_validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ these cases you can set the ``validation_groups`` option to ``false``::

use Symfony\Component\OptionsResolver\OptionsResolver;

public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'validation_groups' => false,
Expand Down
Loading

0 comments on commit aec1016

Please sign in to comment.