Forms.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form;
  11. /**
  12. * Entry point of the Form component.
  13. *
  14. * Use this class to conveniently create new form factories:
  15. *
  16. * use Symfony\Component\Form\Forms;
  17. *
  18. * $formFactory = Forms::createFormFactory();
  19. *
  20. * $form = $formFactory->createBuilder()
  21. * ->add('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType')
  22. * ->add('lastName', 'Symfony\Component\Form\Extension\Core\Type\TextType')
  23. * ->add('age', 'Symfony\Component\Form\Extension\Core\Type\IntegerType')
  24. * ->add('color', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', [
  25. * 'choices' => ['Red' => 'r', 'Blue' => 'b'],
  26. * ])
  27. * ->getForm();
  28. *
  29. * You can also add custom extensions to the form factory:
  30. *
  31. * $formFactory = Forms::createFormFactoryBuilder()
  32. * ->addExtension(new AcmeExtension())
  33. * ->getFormFactory();
  34. *
  35. * If you create custom form types or type extensions, it is
  36. * generally recommended to create your own extensions that lazily
  37. * load these types and type extensions. In projects where performance
  38. * does not matter that much, you can also pass them directly to the
  39. * form factory:
  40. *
  41. * $formFactory = Forms::createFormFactoryBuilder()
  42. * ->addType(new PersonType())
  43. * ->addType(new PhoneNumberType())
  44. * ->addTypeExtension(new FormTypeHelpTextExtension())
  45. * ->getFormFactory();
  46. *
  47. * Support for the Validator component is provided by ValidatorExtension.
  48. * This extension needs a validator object to function properly:
  49. *
  50. * use Symfony\Component\Validator\Validation;
  51. * use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
  52. *
  53. * $validator = Validation::createValidator();
  54. * $formFactory = Forms::createFormFactoryBuilder()
  55. * ->addExtension(new ValidatorExtension($validator))
  56. * ->getFormFactory();
  57. *
  58. * @author Bernhard Schussek <bschussek@gmail.com>
  59. */
  60. final class Forms
  61. {
  62. /**
  63. * Creates a form factory with the default configuration.
  64. *
  65. * @return FormFactoryInterface The form factory
  66. */
  67. public static function createFormFactory(): FormFactoryInterface
  68. {
  69. return self::createFormFactoryBuilder()->getFormFactory();
  70. }
  71. /**
  72. * Creates a form factory builder with the default configuration.
  73. *
  74. * @return FormFactoryBuilderInterface The form factory builder
  75. */
  76. public static function createFormFactoryBuilder(): FormFactoryBuilderInterface
  77. {
  78. return new FormFactoryBuilder(true);
  79. }
  80. /**
  81. * This class cannot be instantiated.
  82. */
  83. private function __construct()
  84. {
  85. }
  86. }