PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/silex/silex/doc/providers/form.rst

https://github.com/brtriver/silx-users-jp
ReStructuredText | 214 lines | 148 code | 66 blank | 0 comment | 0 complexity | 30df6a6e9ae64b16248741c2c16fcab0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. FormServiceProvider
  2. ===================
  3. The *FormServiceProvider* provides a service for building forms in
  4. your application with the Symfony2 Form component.
  5. Parameters
  6. ----------
  7. * **form.secret**: This secret value is used for generating and validating the
  8. CSRF token for a specific page. It is very important for you to set this
  9. value to a static randomly generated value, to prevent hijacking of your
  10. forms. Defaults to ``md5(__DIR__)``.
  11. Services
  12. --------
  13. * **form.factory**: An instance of `FormFactory
  14. <http://api.symfony.com/master/Symfony/Component/Form/FormFactory.html>`_,
  15. that is used for build a form.
  16. * **form.csrf_provider**: An instance of an implementation of the
  17. `CsrfProviderInterface
  18. <http://api.symfony.com/master/Symfony/Component/Form/Extension/Csrf/CsrfProvider/CsrfProviderInterface.html>`_,
  19. defaults to a `DefaultCsrfProvider
  20. <http://api.symfony.com/master/Symfony/Component/Form/Extension/Csrf/CsrfProvider/DefaultCsrfProvider.html>`_.
  21. Registering
  22. -----------
  23. .. code-block:: php
  24. use Silex\Provider\FormServiceProvider;
  25. $app->register(new FormServiceProvider());
  26. .. note::
  27. If you don't want to create your own form layout, it's fine: a default one
  28. will be used. But you will have to register the :doc:`translation provider
  29. <translation>` as the default form layout requires it.
  30. If you want to use validation with forms, do not forget to register the
  31. :doc:`Validator provider <validator>`.
  32. .. note::
  33. The Symfony Form Component and all its dependencies (optional or not) comes
  34. with the "fat" Silex archive but not with the regular one.
  35. If you are using Composer, add it as a dependency to your
  36. ``composer.json`` file:
  37. .. code-block:: json
  38. "require": {
  39. "symfony/form": "~2.3"
  40. }
  41. If you are going to use the validation extension with forms, you must also
  42. add a dependency to the ``symfony/config`` and ``symfony/translation``
  43. components:
  44. .. code-block:: json
  45. "require": {
  46. "symfony/validator": "~2.3",
  47. "symfony/config": "~2.3",
  48. "symfony/translation": "~2.3"
  49. }
  50. The Symfony Form Component relies on the PHP intl extension. If you don't have
  51. it, you can install the Symfony Locale Component as a replacement:
  52. .. code-block:: json
  53. "require": {
  54. "symfony/locale": "~2.3"
  55. }
  56. The Symfony Security CSRF component is used to protect forms against CSRF attacks:
  57. .. code-block:: json
  58. "require": {
  59. "symfony/security-csrf": "~2.4"
  60. }
  61. If you want to use forms in your Twig templates, make sure to install the
  62. Symfony Twig Bridge:
  63. .. code-block:: json
  64. "require": {
  65. "symfony/twig-bridge": "~2.3"
  66. }
  67. Usage
  68. -----
  69. The FormServiceProvider provides a ``form.factory`` service. Here is a usage
  70. example::
  71. $app->match('/form', function (Request $request) use ($app) {
  72. // some default data for when the form is displayed the first time
  73. $data = array(
  74. 'name' => 'Your name',
  75. 'email' => 'Your email',
  76. );
  77. $form = $app['form.factory']->createBuilder('form', $data)
  78. ->add('name')
  79. ->add('email')
  80. ->add('gender', 'choice', array(
  81. 'choices' => array(1 => 'male', 2 => 'female'),
  82. 'expanded' => true,
  83. ))
  84. ->getForm();
  85. $form->handleRequest($request);
  86. if ($form->isValid()) {
  87. $data = $form->getData();
  88. // do something with the data
  89. // redirect somewhere
  90. return $app->redirect('...');
  91. }
  92. // display the form
  93. return $app['twig']->render('index.twig', array('form' => $form->createView()));
  94. });
  95. And here is the ``index.twig`` form template (requires ``symfony/twig-bridge``):
  96. .. code-block:: jinja
  97. <form action="#" method="post">
  98. {{ form_widget(form) }}
  99. <input type="submit" name="submit" />
  100. </form>
  101. If you are using the validator provider, you can also add validation to your
  102. form by adding constraints on the fields::
  103. use Symfony\Component\Validator\Constraints as Assert;
  104. $app->register(new Silex\Provider\ValidatorServiceProvider());
  105. $app->register(new Silex\Provider\TranslationServiceProvider(), array(
  106. 'translator.domains' => array(),
  107. ));
  108. $form = $app['form.factory']->createBuilder('form')
  109. ->add('name', 'text', array(
  110. 'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 5)))
  111. ))
  112. ->add('email', 'text', array(
  113. 'constraints' => new Assert\Email()
  114. ))
  115. ->add('gender', 'choice', array(
  116. 'choices' => array(1 => 'male', 2 => 'female'),
  117. 'expanded' => true,
  118. 'constraints' => new Assert\Choice(array(1, 2)),
  119. ))
  120. ->getForm();
  121. You can register form types by extending ``form.types``::
  122. $app['form.types'] = $app->share($app->extend('form.types', function ($types) use ($app) {
  123. $types[] = new YourFormType();
  124. return $types;
  125. }));
  126. You can register form extensions by extending ``form.extensions``::
  127. $app['form.extensions'] = $app->share($app->extend('form.extensions', function ($extensions) use ($app) {
  128. $extensions[] = new YourTopFormExtension();
  129. return $extensions;
  130. }));
  131. You can register form type extensions by extending ``form.type.extensions``::
  132. $app['form.type.extensions'] = $app->share($app->extend('form.type.extensions', function ($extensions) use ($app) {
  133. $extensions[] = new YourFormTypeExtension();
  134. return $extensions;
  135. }));
  136. You can register form type guessers by extending ``form.type.guessers``::
  137. $app['form.type.guessers'] = $app->share($app->extend('form.type.guessers', function ($guessers) use ($app) {
  138. $guessers[] = new YourFormTypeGuesser();
  139. return $guessers;
  140. }));
  141. Traits
  142. ------
  143. ``Silex\Application\FormTrait`` adds the following shortcuts:
  144. * **form**: Creates a FormBuilder instance.
  145. .. code-block:: php
  146. $app->form($data);
  147. For more information, consult the `Symfony2 Forms documentation
  148. <http://symfony.com/doc/2.3/book/forms.html>`_.