10000 Found more places which use old form types · symfony/symfony-docs@f8b080d · GitHub
[go: up one dir, main page]

Skip to content

Commit f8b080d

Browse files
committed
Found more places which use old form types
1 parent 3237a34 commit f8b080d

File tree

11 files changed

+189
-120
lines changed

11 files changed

+189
-120
lines changed

best_practices/forms.rst

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@ form in its own PHP class::
2222
use Symfony\Component\Form\AbstractType;
2323
use Symfony\Component\Form\FormBuilderInterface;
2424
use Symfony\Component\OptionsResolver\OptionsResolver;
25+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
26+
use Symfony\Component\Form\Extension\Core\Type\EmailType;
27+
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
2528

2629
class PostType extends AbstractType
2730
{
2831
public function buildForm(FormBuilderInterface $builder, array $options)
2932
{
3033
$builder
3134
->add('title')
32-
->add('summary', 'textarea')
33-
->add('content', 'textarea')
34-
->add('authorEmail', 'email')
35-
->add('publishedAt', 'datetime')
35+
->add('summary', TextareaType::class)
36+
->add('content', TextareaType::class)
37+
->add('authorEmail', EmailType::class)
38+
->add('publishedAt', DateTimeType::class)
3639
;
3740
}
3841

@@ -42,22 +45,17 @@ form in its own PHP class::
4245
'data_class' => 'AppBundle\Entity\Post'
4346
));
4447
}
45-
46-
public function getName()
47-
{
48-
return 'post';
49-
}
5048
}
5149

52-
To use the class, use ``createForm`` and instantiate the new class::
50+
To use the class, use ``createForm`` and pass the fully qualified class name::
5351

5452
use AppBundle\Form\PostType;
5553
// ...
5654

5755
public function newAction(Request $request)
5856
{
5957
$post = new Post();
60-
$form = $this->createForm(new PostType(), $post);
58+
$form = $this->createForm(PostType::class, $post);
6159

6260
// ...
6361
}
@@ -97,7 +95,7 @@ directly in your form class, this would effectively limit the scope of that form
9795
{
9896
$builder
9997
// ...
100-
->add('save', 'submit', array('label' => 'Create Post'))
98+
->add('save', SubmitType::class, array('label' => 'Create Post'))
10199
;
102100
}
103101
@@ -122,7 +120,7 @@ some developers configure form buttons in the controller::
122120
public function newAction(Request $request)
123121
{
124122
$post = new Post();
125-
$form = $this->createForm(new PostType(), $post);
123+
$form = $this->createForm(PostType::class, $post);
126124
$form->add('submit', 'submit', array(
127125
'label' => 'Create',
128126
'attr' => array('class' => 'btn btn-default pull-right')

components/form/form_events.rst

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,13 @@ Creating and binding an event listener to the form is very easy::
288288

289289
use Symfony\Component\Form\FormEvent;
290290
use Symfony\Component\Form\FormEvents;
291+
use Symfony\Component\Form\Extension\Core\Type\TextType;
292+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
293+
use Symfony\Component\Form\Extension\Core\Type\EmailType;
291294

292295
$form = $formFactory->createBuilder()
293-
->add('username', 'text')
294-
->add('show_email', 'checkbox')
296+
->add('username', TextType::class)
297+
->add('show_email', CheckboxType::class)
295298
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
296299
$user = $event->getData();
297300
$form = $event->getForm();
@@ -304,7 +307,7 @@ Creating and binding an event listener to the form is very easy::
304307
// If the data was submitted previously, the additional value that is
305308
// included in the request variables needs to be removed.
306309
if (true === $user['show_email']) {
307-
$form->add('email', 'email');
310+
$form->add('email', EmailType::class
308311
} else {
309312
unset($user['email']);
310313
$event->setData($user);
@@ -317,14 +320,17 @@ Creating and binding an event listener to the form is very easy::
317320
When you have created a form type class, you can use one of its methods as a
318321
callback for better readability::
319322

323+
use Symfony\Component\Form\Extension\Core\Type\TextType;
324+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
325+
320326
// ...
321327

322328
class SubscriptionType extends AbstractType
323329
{
324330
public function buildForm(FormBuilderInterface $builder, array $options)
325331
{
326-
$builder->add('username', 'text');
327-
$builder->add('show_email', 'checkbox');
332+
$builder->add('username', TextType::class);
333+
$builder->add('show_email', CheckboxType::class);
328334
$builder->addEventListener(
329335
FormEvents::PRE_SET_DATA,
330336
array($this, 'onPreSetData')
@@ -351,6 +357,7 @@ Event subscribers have different uses:
351357
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
352358
use Symfony\Component\Form\FormEvent;
353359
use Symfony\Component\Form\FormEvents;
360+
use Symfony\Component\Form\Extension\Core\Type\EmailType;
354361
355362
class AddEmailFieldListener implements EventSubscriberInterface
356363
{
@@ -370,7 +377,7 @@ Event subscribers have different uses:
370377
// Check whether the user from the initial data has chosen to
371378
// display his email or not.
372379
if (true === $user->isShowEmail()) {
373-
$form->add('email', 'email');
380+
$form->add('email', EmailType::class);
374381
}
375382
}
376383
@@ -387,7 +394,7 @@ Event subscribers have different uses:
387394
// If the data was submitted previously, the additional value that
388395
// is included in the request variables needs to be removed.
389396
if (true === $user['show_email']) {
390-
$form->add('email', 'email');
397+
$form->add('email', EmailType::class);
391398
} else {
392399
unset($user['email']);
393400
$event->setData($user);
@@ -397,11 +404,14 @@ Event subscribers have different uses:
397404
398405
To register the event subscriber, use the addEventSubscriber() method::
399406

407+
use Symfony\Component\Form\Extension\Core\Type\TextType;
408+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
409+
400410
// ...
401411

402412
$form = $formFactory->createBuilder()
403-
->add('username', 'text')
404-
->add('show_email', 'checkbox')
413+
->add('username', TextType::class)
414+
->add('show_email', CheckboxType::class)
405415
->addEventSubscriber(new AddEmailFieldListener())
406416
->getForm();
407417

components/form/type_guesser.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ With this knowledge, you can easily implement the ``guessType`` method of the
9191

9292
use Symfony\Component\Form\Guess\Guess;
9393
use Symfony\Component\Form\Guess\TypeGuess;
94+
use Symfony\Component\Form\Extension\Core\Type\TextType;
95+
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
96+
use Symfony\Component\Form\Extension\Core\Type\NumberType;
97+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
9498

9599
class PHPDocTypeGuesser implements FormTypeGuesserInterface
96100
{
@@ -107,25 +111,25 @@ With this knowledge, you can easily implement the ``guessType`` method of the
107111
case 'string':
108112
// there is a high confidence that the type is text when
109113
// @var string is used
110-
return new TypeGuess('text', array(), Guess::HIGH_CONFIDENCE);
114+
return new TypeGuess(TextType::class, array(), Guess::HIGH_CONFIDENCE);
111115

112116
case 'int':
113117
case 'integer':
114118
// integers can also be the id of an entity or a checkbox (0 or 1)
115-
return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE);
119+
return new TypeGuess(IntegerType::class, array(), Guess::MEDIUM_CONFIDENCE);
116120

117121
case 'float':
118122
case 'double':
119123
case 'real':
120-
return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
124+
return new TypeGuess(NumberType::class, array(), Guess::MEDIUM_CONFIDENCE);
121125

122126
case 'boolean':
123127
case 'bool':
124-
return new TypeGuess('checkbox', array(), Guess::HIGH_CONFIDENCE);
128+
return new TypeGuess(CheckboxType::class, array(), Guess::HIGH_CONFIDENCE);
125129

126130
default:
127131
// there is a very low confidence that this one is correct
128-
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
132+
return new TypeGuess(TextType::class, array(), Guess::LOW_CONFIDENCE);
129133
}
130134
}
131135

cookbook/doctrine/registration_form.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,18 @@ Next, create the form for the ``User`` entity::
157157
use Symfony\Component\Form\AbstractType;
158158
use Symfony\Component\Form\FormBuilderInterface;
159159
use Symfony\Component\OptionsResolver\OptionsResolver;
160+
use Symfony\Component\Form\Extension\Core\Type\EmailType;
161+
use Symfony\Component\Form\Extension\Core\Type\TextType;
162+
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
160163

161164
class UserType extends AbstractType
162165
{
163166
public function buildForm(FormBuilderInterface $builder, array $options)
164167
{
165168
$builder
166-
->add('email', 'email')
167-
->add('username', 'text')
168-
->add('plainPassword', 'repeated', array(
169+
->add('email', EmailType::class)
170+
->add('username', TextType::class)
171+
->add('plainPassword', RepeatedType::class, array(
169172
'type' => 'password',
170173
'first_options' => array('label' => 'Password'),
171174
'second_options' => array('label' => 'Repeat Password'),
@@ -213,7 +216,7 @@ controller for displaying the registration form::
213216
{
214217
// 1) build the form
215218
$user = new User();
216-
$form = $this->createForm(new UserType(), $user);
219+
$form = $this->createForm(UserType::class, $user);
217220

218221
// 2) handle the submit (will only happen on POST)
219222
$form->handleRequest($request);
@@ -368,15 +371,17 @@ To do this, add a ``termsAccepted`` field to your form, but set its
368371
// src/AppBundle/Form/UserType.php
369372
// ...
370373
use Symfony\Component\Validator\Constraints\IsTrue;
374+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
375+
use Symfony\Component\Form\Extension\Core\Type\EmailType;
371376

372377
class UserType extends AbstractType
373378
{
374379
public function buildForm(FormBuilderInterface $builder, array $options)
375380
{
376381
$builder
377-
->add('email', 'email');
382+
->add('email', EmailType::class);
378383
// ...
379-
->add('termsAccepted', 'checkbox', array(
384+
->add('termsAccepted', CheckboxType::class, array(
380385
'mapped' => false,
381386
'constraints' => new IsTrue(),
382387
))

cookbook/form/create_form_type_extension.rst

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,16 @@ next to the file field. For example::
300300

301301
use Symfony\Component\Form\AbstractType;
302302
use Symfony\Component\Form\FormBuilderInterface;
303+
use Symfony\Component\Form\Extension\Core\Type\TextType;
304+
use Symfony\Component\Form\Extension\Core\Type\FileType;
303305

304306
class MediaType extends AbstractT 10000 ype
305307
{
306308
public function buildForm(FormBuilderInterface $builder, array $options)
307309
{
308310
$builder
309-
->add('name', 'text')
310-
->add('file', 'file', array('image_path' => 'webPath'));
311+
->add('name', TextType::class)
312+
->add('file', FileType::class, array('image_path' => 'webPath'));
311313
}
312314

313315
public function getName()
@@ -324,13 +326,13 @@ Generic Form Type Extensions
324326

325327
You can modify several form types at once by specifying their common parent
326328
(:doc:`/reference/forms/types`). For example, several form types natively
327-
available in Symfony inherit from the ``text`` form type (such as ``email``,
328-
``search``, ``url``, etc.). A form type extension applying to ``text``
329-
(i.e. whose ``getExtendedType`` method returns ``text``) would apply to all of
330-
these form types.
329+
available in Symfony inherit from the ``TextType`` form type (such as ``email``,
330+
``SearchType``, ``UrlType``, etc.). A form type extension applying to ``TextType``
331+
(i.e. whose ``getExtendedType`` method returns ``TextType::class``) would apply
332+
to all of these form types.
331333

332334
In the same way, since **most** form types natively available in Symfony inherit
333-
from the ``form`` form type, a form type extension applying to ``form`` would
334-
apply to all of these. A notable exception are the ``button`` form types. Also
335-
keep in mind that a custom form type which extends neither the ``form`` nor
336-
the ``button`` type could always be created.
335+
from the ``FormType`` form type, a form type extension applying to ``FormType``
336+
would apply to all of these. A notable exception are the ``ButtonType`` form
337+
types. Also keep in mind that a custom form type which extends neither the
338+
``FormType`` nor the ``ButtonType`` type could always be created.

0 commit comments

Comments
 (0)
0