@@ -24,25 +24,26 @@ Create a PHPDoc Type Guesser
2424In this section, you are going to build a guesser that reads information about
2525fields from the PHPDoc of the properties. At first, you need to create a class
2626which 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
4141Start 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
7374that 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
145148This type guesser can now guess the field type for a property if it has
@@ -148,8 +151,8 @@ PHPdoc!
148151Guessing 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 `
153156instance with the value of the option. This constructor has 2 arguments:
154157
155158* The value of the option;
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.
170173Registering 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+ // ...
0 commit comments