Forms.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. use Symfony\Component\Form\Extension\Core\CoreExtension;
  12. /**
  13. * Entry point of the Form component.
  14. *
  15. * Use this class to conveniently create new form factories:
  16. *
  17. * use Symfony\Component\Form\Forms;
  18. *
  19. * $formFactory = Forms::createFormFactory();
  20. *
  21. * $form = $formFactory->createBuilder()
  22. * ->add('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType')
  23. * ->add('lastName', 'Symfony\Component\Form\Extension\Core\Type\TextType')
  24. * ->add('age', 'Symfony\Component\Form\Extension\Core\Type\IntegerType')
  25. * ->add('gender', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array(
  26. * 'choices' => array('Male' => 'm', 'Female' => 'f'),
  27. * 'choices_as_values' => true,
  28. * ))
  29. * ->getForm();
  30. *
  31. * You can also add custom extensions to the form factory:
  32. *
  33. * $formFactory = Forms::createFormFactoryBuilder()
  34. * ->addExtension(new AcmeExtension())
  35. * ->getFormFactory();
  36. *
  37. * If you create custom form types or type extensions, it is
  38. * generally recommended to create your own extensions that lazily
  39. * load these types and type extensions. In projects where performance
  40. * does not matter that much, you can also pass them directly to the
  41. * form factory:
  42. *
  43. * $formFactory = Forms::createFormFactoryBuilder()
  44. * ->addType(new PersonType())
  45. * ->addType(new PhoneNumberType())
  46. * ->addTypeExtension(new FormTypeHelpTextExtension())
  47. * ->getFormFactory();
  48. *
  49. * Support for the Validator component is provided by ValidatorExtension.
  50. * This extension needs a validator object to function properly:
  51. *
  52. * use Symfony\Component\Validator\Validation;
  53. * use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
  54. *
  55. * $validator = Validation::createValidator();
  56. * $formFactory = Forms::createFormFactoryBuilder()
  57. * ->addExtension(new ValidatorExtension($validator))
  58. * ->getFormFactory();
  59. *
  60. * Support for the Templating component is provided by TemplatingExtension.
  61. * This extension needs a PhpEngine object for rendering forms. As second
  62. * argument you should pass the names of the default themes. Here is an
  63. * example for using the default layout with "<div>" tags:
  64. *
  65. * use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
  66. *
  67. * $formFactory = Forms::createFormFactoryBuilder()
  68. * ->addExtension(new TemplatingExtension($engine, null, array(
  69. * 'FrameworkBundle:Form',
  70. * )))
  71. * ->getFormFactory();
  72. *
  73. * The next example shows how to include the "<table>" layout:
  74. *
  75. * use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
  76. *
  77. * $formFactory = Forms::createFormFactoryBuilder()
  78. * ->addExtension(new TemplatingExtension($engine, null, array(
  79. * 'FrameworkBundle:Form',
  80. * 'FrameworkBundle:FormTable',
  81. * )))
  82. * ->getFormFactory();
  83. *
  84. * @author Bernhard Schussek <bschussek@gmail.com>
  85. */
  86. final class Forms
  87. {
  88. /**
  89. * Creates a form factory with the default configuration.
  90. *
  91. * @return FormFactoryInterface The form factory
  92. */
  93. public static function createFormFactory()
  94. {
  95. return self::createFormFactoryBuilder()->getFormFactory();
  96. }
  97. /**
  98. * Creates a form factory builder with the default configuration.
  99. *
  100. * @return FormFactoryBuilderInterface The form factory builder
  101. */
  102. public static function createFormFactoryBuilder()
  103. {
  104. $builder = new FormFactoryBuilder();
  105. $builder->addExtension(new CoreExtension());
  106. return $builder;
  107. }
  108. /**
  109. * This class cannot be instantiated.
  110. */
  111. private function __construct()
  112. {
  113. }
  114. }