Skip to content

Commit 9458a09

Browse files
committed
feature symfony#5834 Updated form aliases to FQCNs for forms in book and component (hiddewie)
This PR was merged into the 2.8 branch. Discussion ---------- Updated form aliases to FQCNs for forms in book and component | Q | A |------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.8+ | Fixed tickets | symfony#5588 For the 3.0 branch, all FQCNs should be updated to use the `::class` constant. Commits ------- e103627 Fixed wrong indendation f8b080d Found more places which use old form types 3237a34 Updated form constant usage 3ab3830 Fixed PHP 5.5+ reference in form component 291a42a Fixed removed XML tag in form book 0820e69 Updated form aliases to FQCNs for forms in book and component
2 parents 329182d + e103627 commit 9458a09

13 files changed

+349
-229
lines changed

best_practices/forms.rst

+11-13
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')

0 commit comments

Comments
 (0)