Skip to content

Commit 18d2dbb

Browse files
committed
Moved Type Guesser article to the topic
1 parent 7ed9788 commit 18d2dbb

File tree

4 files changed

+77
-44
lines changed

4 files changed

+77
-44
lines changed

components/form.rst

-9
Original file line numberDiff line numberDiff line change
@@ -702,15 +702,6 @@ method to access the list of errors. It returns a
702702
This is useful, for example, if you want to use PHP's ``array_`` function
703703
on the form errors.
704704

705-
Learn More
706-
----------
707-
708-
.. toctree::
709-
:maxdepth: 1
710-
:glob:
711-
712-
form/*
713-
714705
.. _Packagist: https://packagist.org/packages/symfony/form
715706
.. _Twig: http://twig.sensiolabs.org
716707
.. _`Twig Configuration`: http://twig.sensiolabs.org/doc/intro.html

components/form/type_guesser.rst renamed to form/type_guesser.rst

+75-34
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,26 @@ Create a PHPDoc Type Guesser
2424
In this section, you are going to build a guesser that reads information about
2525
fields from the PHPDoc of the properties. At first, you need to create a class
2626
which implements :class:`Symfony\\Component\\Form\\FormTypeGuesserInterface`.
27-
This interface requires 4 methods:
28-
29-
* :method:`Symfony\\Component\\Form\\FormTypeGuesserInterface::guessType` -
30-
tries to guess the type of a field;
31-
* :method:`Symfony\\Component\\Form\\FormTypeGuesserInterface::guessRequired` -
32-
tries to guess the value of the :ref:`required <reference-form-option-required>`
33-
option;
34-
* :method:`Symfony\\Component\\Form\\FormTypeGuesserInterface::guessMaxLength` -
35-
tries to guess the value of the :ref:`max_length <reference-form-option-max_length>`
36-
option;
37-
* :method:`Symfony\\Component\\Form\\FormTypeGuesserInterface::guessPattern` -
38-
tries to guess the value of the :ref:`pattern <reference-form-option-pattern>`
39-
option.
27+
This interface requires four methods:
28+
29+
:method:`Symfony\\Component\\Form\\FormTypeGuesserInterface::guessType`
30+
Tries to guess the type of a field;
31+
:method:`Symfony\\Component\\Form\\FormTypeGuesserInterface::guessRequired`
32+
Tries to guess the value of the :ref:`required <reference-form-option-required>`
33+
option;
34+
:method:`Symfony\\Component\\Form\\FormTypeGuesserInterface::guessMaxLength`
35+
Tries to guess the value of the :ref:`max_length <reference-form-option-max_length>`
36+
option;
37+
:method:`Symfony\\Component\\Form\\FormTypeGuesserInterface::guessPattern`
38+
Tries to guess the value of the :ref:`pattern <reference-form-option-pattern>`
39+
option.
4040

4141
Start by creating the class and these methods. Next, you'll learn how to fill each on.
4242

4343
.. code-block:: php
4444
45-
namespace Acme\Form;
45+
// src/AppBundle/Form/TypeGuesser/PHPDocTypeGuesser.php
46+
namespace AppBundle\Form\TypeGuesser;
4647
4748
use Symfony\Component\Form\FormTypeGuesserInterface;
4849
@@ -72,7 +73,7 @@ When guessing a type, the method returns either an instance of
7273
:class:`Symfony\\Component\\Form\\Guess\\TypeGuess` or nothing, to determine
7374
that the type guesser cannot guess the type.
7475

75-
The ``TypeGuess`` constructor requires 3 options:
76+
The ``TypeGuess`` constructor requires three options:
7677

7778
* The type name (one of the :doc:`form types </reference/forms/types>`);
7879
* Additional options (for instance, when the type is ``entity``, you also
@@ -84,10 +85,10 @@ The ``TypeGuess`` constructor requires 3 options:
8485
``VERY_HIGH_CONFIDENCE``. After all type guessers have been executed, the
8586
type with the highest confidence is used.
8687

87-
With this knowledge, you can easily implement the ``guessType`` method of the
88+
With this knowledge, you can easily implement the ``guessType()`` method of the
8889
``PHPDocTypeGuesser``::
8990

90-
namespace Acme\Form;
91+
namespace AppBundle\Form\TypeGuesser;
9192

9293
use Symfony\Component\Form\Guess\Guess;
9394
use Symfony\Component\Form\Guess\TypeGuess;
@@ -140,6 +141,8 @@ With this knowledge, you can easily implement the ``guessType`` method of the
140141

141142
return $phpdocTags;
142143
}
144+
145+
// ...
143146
}
144147

145148
This type guesser can now guess the field type for a property if it has
@@ -148,8 +151,8 @@ PHPdoc!
148151
Guessing Field Options
149152
~~~~~~~~~~~~~~~~~~~~~~
150153

151-
The other 3 methods (``guessMaxLength``, ``guessRequired`` and
152-
``guessPattern``) return a :class:`Symfony\\Component\\Form\\Guess\\ValueGuess`
154+
The other three methods (``guessMaxLength()``, ``guessRequired()`` and
155+
``guessPattern()``) return a :class:`Symfony\\Component\\Form\\Guess\\ValueGuess`
153156
instance with the value of the option. This constructor has 2 arguments:
154157

155158
* The value of the option;
@@ -161,7 +164,7 @@ set.
161164

162165
.. caution::
163166

164-
You should be very careful using the ``guessPattern`` method. When the
167+
You should be very careful using the ``guessPattern()`` method. When the
165168
type is a float, you cannot use it to determine a min or max value of the
166169
float (e.g. you want a float to be greater than ``5``, ``4.512313`` is not valid
167170
but ``length(4.512314) > length(5)`` is, so the pattern will succeed). In
@@ -170,22 +173,60 @@ set.
170173
Registering a Type Guesser
171174
--------------------------
172175

173-
The last thing you need to do is registering your custom type guesser by using
174-
:method:`Symfony\\Component\\Form\\FormFactoryBuilder::addTypeGuesser` or
175-
:method:`Symfony\\Component\\Form\\FormFactoryBuilder::addTypeGuessers`::
176176

177-
use Symfony\Component\Form\Forms;
178-
use Acme\Form\PHPDocTypeGuesser;
177+
The last thing you need to do is registering your custom type guesser by
178+
creating a service and tagging it as ``form.type_guesser``:
179179

180-
$formFactory = Forms::createFormFactoryBuilder()
181-
// ...
182-
->addTypeGuesser(new PHPDocTypeGuesser())
183-
->getFormFactory();
180+
.. configuration-block::
181+
182+
.. code-block:: yaml
183+
184+
# app/config/services.yml
185+
services:
186+
187+
app.phpdoc_type_guesser:
188+
class: AppBundle\Form\TypeGuesser\PHPDocTypeGuesser
189+
tags:
190+
- { name: form.type_guesser }
191+
192+
.. code-block:: xml
193+
194+
<!-- app/config/services.xml -->
195+
<?xml version="1.0" encoding="UTF-8" ?>
196+
<container xmlns="http://symfony.com/schema/dic/services"
197+
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
198+
xsd:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
199+
>
200+
<services>
184201
185-
// ...
202+
<service class="AppBundle\Form\TypeGuesser\PHPDocTypeGuesser">
203+
<tag name="form.type_guesser"/>
204+
</service>
186205
187-
.. note::
206+
</services>
207+
</container>
188208
189-
When you use the Symfony Framework, you need to register your type guesser
190-
and tag it with ``form.type_guesser``. For more information see
191-
:ref:`the tag reference <reference-dic-type_guesser>`.
209+
.. code-block:: php
210+
211+
// app/config/services.php
212+
$container->register('AppBundle\Form\TypeGuesser\PHPDocTypeGuesser')
213+
->addTag('form.type_guesser')
214+
;
215+
216+
217+
.. sidebar:: Registering a Type Guesser in the Component
218+
219+
If you're using the Form component standalone in your PHP project, use
220+
:method:`Symfony\\Component\\Form\\FormFactoryBuilder::addTypeGuesser` or
221+
:method:`Symfony\\Component\\Form\\FormFactoryBuilder::addTypeGuessers` of
222+
the ``FormFactoryBuilder`` to register new type guessers::
223+
224+
use Symfony\Component\Form\Forms;
225+
use Acme\Form\PHPDocTypeGuesser;
226+
227+
$formFactory = Forms::createFormFactoryBuilder()
228+
// ...
229+
->addTypeGuesser(new PHPDocTypeGuesser())
230+
->getFormFactory();
231+
232+
// ...

redirection_map

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@
257257
/components/form/form_events /form/events
258258
/components/form/introduction /components/form
259259
/components/form/index /components/form
260+
/components/form/type_guesser /form/type_guesser
260261
/components/http_foundation/introduction /components/http_foundation
261262
/components/http_kernel/introduction /components/http_kernel
262263
/components/http_kernel/index /components/http_kernel

reference/dic_tags.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ metadata and Doctrine metadata (if you're using Doctrine) or Propel metadata
410410
.. seealso::
411411

412412
For information on how to create your own type guesser, see
413-
:doc:`/components/form/type_guesser`.
413+
:doc:`/form/type_guesser`.
414414

415415
kernel.cache_clearer
416416
--------------------

0 commit comments

Comments
 (0)