Skip to content

Commit

Permalink
Found more places which use old form types
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddewie committed Nov 23, 2015
1 parent 3237a34 commit f8b080d
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 120 deletions.
24 changes: 11 additions & 13 deletions best_practices/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ form in its own PHP class::
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;

class PostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('summary', 'textarea')
->add('content', 'textarea')
->add('authorEmail', 'email')
->add('publishedAt', 'datetime')
->add('summary', TextareaType::class)
->add('content', TextareaType::class)
->add('authorEmail', EmailType::class)
->add('publishedAt', DateTimeType::class)
;
}

Expand All @@ -42,22 +45,17 @@ form in its own PHP class::
'data_class' => 'AppBundle\Entity\Post'
));
}

public function getName()
{
return 'post';
}
}

To use the class, use ``createForm`` and instantiate the new class::
To use the class, use ``createForm`` and pass the fully qualified class name::

use AppBundle\Form\PostType;
// ...

public function newAction(Request $request)
{
$post = new Post();
$form = $this->createForm(new PostType(), $post);
$form = $this->createForm(PostType::class, $post);

// ...
}
Expand Down Expand Up @@ -97,7 +95,7 @@ directly in your form class, this would effectively limit the scope of that form
{
$builder
// ...
->add('save', 'submit', array('label' => 'Create Post'))
->add('save', SubmitType::class, array('label' => 'Create Post'))
;
}
Expand All @@ -122,7 +120,7 @@ some developers configure form buttons in the controller::
public function newAction(Request $request)
{
$post = new Post();
$form = $this->createForm(new PostType(), $post);
$form = $this->createForm(PostType::class, $post);
$form->add('submit', 'submit', array(
'label' => 'Create',
'attr' => array('class' => 'btn btn-default pull-right')
Expand Down
28 changes: 19 additions & 9 deletions components/form/form_events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,13 @@ Creating and binding an event listener to the form is very easy::

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;

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

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;

// ...

class SubscriptionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('username', 'text');
$builder->add('show_email', 'checkbox');
$builder->add('username', TextType::class);
$builder->add('show_email', CheckboxType::class);
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
array($this, 'onPreSetData')
Expand All @@ -351,6 +357,7 @@ Event subscribers have different uses:
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
class AddEmailFieldListener implements EventSubscriberInterface
{
Expand All @@ -370,7 +377,7 @@ Event subscribers have different uses:
// Check whether the user from the initial data has chosen to
// display his email or not.
if (true === $user->isShowEmail()) {
$form->add('email', 'email');
$form->add('email', EmailType::class);
}
}
Expand All @@ -387,7 +394,7 @@ Event subscribers have different uses:
// If the data was submitted previously, the additional value that
// is included in the request variables needs to be removed.
if (true === $user['show_email']) {
$form->add('email', 'email');
$form->add('email', EmailType::class);
} else {
unset($user['email']);
$event->setData($user);
Expand All @@ -397,11 +404,14 @@ Event subscribers have different uses:
To register the event subscriber, use the addEventSubscriber() method::

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;

// ...

$form = $formFactory->createBuilder()
->add('username', 'text')
->add('show_email', 'checkbox')
->add('username', TextType::class)
->add('show_email', CheckboxType::class)
->addEventSubscriber(new AddEmailFieldListener())
->getForm();

Expand Down
14 changes: 9 additions & 5 deletions components/form/type_guesser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ With this knowledge, you can easily implement the ``guessType`` method of the

use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;

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

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

case 'float':
case 'double':
case 'real':
return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
return new TypeGuess(NumberType::class, array(), Guess::MEDIUM_CONFIDENCE);

case 'boolean':
case 'bool':
return new TypeGuess('checkbox', array(), Guess::HIGH_CONFIDENCE);
return new TypeGuess(CheckboxType::class, array(), Guess::HIGH_CONFIDENCE);

default:
// there is a very low confidence that this one is correct
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
return new TypeGuess(TextType::class, array(), Guess::LOW_CONFIDENCE);
}
}

Expand Down
17 changes: 11 additions & 6 deletions cookbook/doctrine/registration_form.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,18 @@ Next, create the form for the ``User`` entity::
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;

class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email')
->add('username', 'text')
->add('plainPassword', 'repeated', array(
->add('email', EmailType::class)
->add('username', TextType::class)
->add('plainPassword', RepeatedType::class, array(
'type' => 'password',
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
Expand Down Expand Up @@ -213,7 +216,7 @@ controller for displaying the registration form::
{
// 1) build the form
$user = new User();
$form = $this->createForm(new UserType(), $user);
$form = $this->createForm(UserType::class, $user);

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

class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email');
->add('email', EmailType::class);
// ...
->add('termsAccepted', 'checkbox', array(
->add('termsAccepted', CheckboxType::class, array(
'mapped' => false,
'constraints' => new IsTrue(),
))
Expand Down
22 changes: 12 additions & 10 deletions cookbook/form/create_form_type_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,16 @@ next to the file field. For example::

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class MediaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('file', 'file', array('image_path' => 'webPath'));
->add('name', TextType::class)
->add('file', FileType::class, array('image_path' => 'webPath'));
}

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

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

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

0 comments on commit f8b080d

Please sign in to comment.