@@ -24,25 +24,26 @@ Create a PHPDoc Type Guesser
24
24
In this section, you are going to build a guesser that reads information about
25
25
fields from the PHPDoc of the properties. At first, you need to create a class
26
26
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.
40
40
41
41
Start by creating the class and these methods. Next, you'll learn how to fill each on.
42
42
43
43
.. code-block :: php
44
44
45
- namespace Acme\Form;
45
+ // src/AppBundle/Form/TypeGuesser/PHPDocTypeGuesser.php
46
+ namespace AppBundle\Form\TypeGuesser;
46
47
47
48
use Symfony\Component\Form\FormTypeGuesserInterface;
48
49
@@ -72,7 +73,7 @@ When guessing a type, the method returns either an instance of
72
73
:class: `Symfony\\ Component\\ Form\\ Guess\\ TypeGuess ` or nothing, to determine
73
74
that the type guesser cannot guess the type.
74
75
75
- The ``TypeGuess `` constructor requires 3 options:
76
+ The ``TypeGuess `` constructor requires three options:
76
77
77
78
* The type name (one of the :doc: `form types </reference/forms/types >`);
78
79
* Additional options (for instance, when the type is ``entity ``, you also
@@ -84,10 +85,10 @@ The ``TypeGuess`` constructor requires 3 options:
84
85
``VERY_HIGH_CONFIDENCE ``. After all type guessers have been executed, the
85
86
type with the highest confidence is used.
86
87
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
88
89
``PHPDocTypeGuesser ``::
89
90
90
- namespace Acme \Form;
91
+ namespace AppBundle \Form\TypeGuesser ;
91
92
92
93
use Symfony\Component\Form\Guess\Guess;
93
94
use Symfony\Component\Form\Guess\TypeGuess;
@@ -140,6 +141,8 @@ With this knowledge, you can easily implement the ``guessType`` method of the
140
141
141
142
return $phpdocTags;
142
143
}
144
+
145
+ // ...
143
146
}
144
147
145
148
This type guesser can now guess the field type for a property if it has
@@ -148,8 +151,8 @@ PHPdoc!
148
151
Guessing Field Options
149
152
~~~~~~~~~~~~~~~~~~~~~~
150
153
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 `
153
156
instance with the value of the option. This constructor has 2 arguments:
154
157
155
158
* The value of the option;
161
164
162
165
.. caution ::
163
166
164
- You should be very careful using the ``guessPattern `` method. When the
167
+ You should be very careful using the ``guessPattern() `` method. When the
165
168
type is a float, you cannot use it to determine a min or max value of the
166
169
float (e.g. you want a float to be greater than ``5 ``, ``4.512313 `` is not valid
167
170
but ``length(4.512314) > length(5) `` is, so the pattern will succeed). In
@@ -170,22 +173,60 @@ set.
170
173
Registering a Type Guesser
171
174
--------------------------
172
175
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 `::
176
176
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 ``:
179
179
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 >
184
201
185
- // ...
202
+ <service class =" AppBundle\Form\TypeGuesser\PHPDocTypeGuesser" >
203
+ <tag name =" form.type_guesser" />
204
+ </service >
186
205
187
- .. note ::
206
+ </services >
207
+ </container >
188
208
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