Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Symfony Forms - Advanced Use Cases

Symfony Forms - Advanced Use Cases

8665aad8f35b1710df79e9aef52d6daa?s=128

Alexandre Salomé

June 17, 2021
Tweet

Transcript

  1. Symfony Forms Advanced Use Cases Alexandre Salomé - 2021

  2. Re-edition

  3. None
  4. Symfony Forms - Use Cases #1: Login #2: Change password

    #3: Sortable fields #4: Security based fields
  5. #1: Login

  6. Symfony Maker Quickly bootstrap a login form:

  7. None
  8. None
  9. None
  10. Controller Creating form from the controller:

  11. Controller Creating form from the controller:

  12. Controller Creating form from the controller:

  13. Controller Creating form from the controller:

  14. Controller Creating form from the controller:

  15. Templating Simplify login template:

  16. Test it No style

  17. Templating Enable bundled templates, add a custom one:

  18. Templating Enable bundled templates, add a custom one:

  19. Templating Enable bundled templates, add a custom one:

  20. Test it Looks good

  21. Test it It fails because of invalid field names.

  22. Form naming $formFactory ->create(LoginType::class) ; login[_username] login[_password] login[_remember_me] login[_csrf_token] $formFactory

    ->createNamed('foo', LoginType::class) ; foo[_username] foo[_password] foo[_remember_me] foo[_csrf_token] $formFactory ->createNamed('', LoginType::class) ; _username _password _remember_me _csrf_token
  23. Creating a form in the controller Configure the name of

    the generated form by using the FormFactory service.
  24. Test it Form names are now correct...

  25. Remember last username We use the “data” argument:

  26. Add error message to the form We add a FormError

    to the Form object:
  27. Test it CSRF token is invalid

  28. Configure CSRF through options

  29. Configure CSRF through options

  30. Changing the Authenticator

  31. Fixed, it works (almost) This field “remember me” is required

    by the browser:
  32. Configuring field options We can configure options for each field:

  33. It works!

  34. Creating a form type (1/2) You can also create a

    dedicated form class type, as explained in documentation.
  35. Creating a form type (2/2) Controller can be simplified

  36. Login Controller consolidated

  37. Login

  38. Login

  39. Login

  40. Login

  41. Form tree

  42. Form tree

  43. Form tree

  44. Login Form - Conclusion - Form manipulation - Type, data

    and options - Form tree - Type hierarchy
  45. #2: Change password

  46. Existing profile page

  47. None
  48. None
  49. None
  50. ChangePasswordType Data is a User object Children - Old password

    - New password repeated Dependency to UserPasswordEncoder
  51. None
  52. None
  53. None
  54. None
  55. None
  56. Adding an event listener

  57. Adding an event listener

  58. Form lifecycle Build Before submission After submission Listeners Write Read

    only Read only Options Write Read only Read only Transformers Write Read only Read only Children & parents Write Write Read only Errors Write Write Write
  59. Form lifecycle Build Before submission After submission Listeners Write Read

    only Read only Options Write Read only Read only Transformers Write Read only Read only Children & parents Write Write Read only Errors Write Write Write PRE_SET_DATA POST_SET_DATA
  60. Form lifecycle Build Before submission After submission Listeners Write Read

    only Read only Options Write Read only Read only Transformers Write Read only Read only Children & parents Write Write Read only Errors Write Write Write PRE_SUBMIT SUBMIT POST_SUBMIT PRE_SET_DATA POST_SET_DATA
  61. Adding an event listener

  62. Inheritance of data in form

  63. Changes in the controller No change to the controller.

  64. Test it

  65. Form tree

  66. Form tree

  67. Form tree

  68. Change password - Conclusion - No change in controller -

    Events & Subscribers - Lifecycle - Relation to the model
  69. #3: Sortable form fields

  70. TodoItem Entity

  71. TodoItemType

  72. TodoListType

  73. Rendering

  74. Form template In _form.html.twig:

  75. Form template In _form.html.twig:

  76. Form rendering

  77. Form rendering ROW ROW ROW ROW ROW ROW

  78. Form rendering WIDGET WIDGET WIDGET WIDGET WIDGET

  79. Form rendering LABEL LABEL LABEL LABEL LABEL LABEL

  80. Form rendering ERRORS

  81. Form rendering ERRORS LABEL LABEL LABEL WIDGET WIDGET LABEL WIDGET

    WIDGET WIDGET LABEL LABEL ROW ROW ROW ROW ROW ROW
  82. Form rendering ROW WIDGET ERRORS LABEL

  83. Form rendering ROW ERRORS LABEL ROW WIDGET ERRORS LABEL ROW

    WIDGET ERRORS LABEL ROW WIDGET ERRORS LABEL
  84. Form template A form template contains block definitions:

  85. Block name resolution Symfony searches for a block name by

    looking at the type hierarchy.
  86. Rendering

  87. SortableType

  88. TodoListType

  89. Encore & Symfony UX

  90. SortableType > Form Template

  91. SortableType > Form Template

  92. sortable_controller.ts

  93. sortable_controller.ts

  94. sortable_controller.ts

  95. sortable_controller.ts

  96. Visible position fields

  97. Hide the field

  98. Rendering

  99. Sortable - Conclusion - Form templating engine - No change

    in page template - Form view model (row/label/widget/error) - Block name resolution logic
  100. #3: Security based fields

  101. A new option “is_granted”

  102. Types & extensions FormType FormTypeExtension buildForm buildView finishView configureOptions getParent

    getExtendedTypes
  103. Creating a new TypeExtension CsrfTypeExtension ValidatorTypeExtension FormType

  104. Creating a new TypeExtension CsrfTypeExtension ValidatorTypeExtension SecurityTypeExtension FormType

  105. Creating a new TypeExtension

  106. Creating a new TypeExtension

  107. Creating a new TypeExtension

  108. Creating a new TypeExtension

  109. None
  110. Security based fields - Conclusion - TypeExtension is similar to

    Type - Allow extending any type
  111. Type Listeners Options mapped virtual Data CollectionType Form tree Root

    Children Form lifecycle Submit Form events Type hierarchy Templating TodoItem TodoList SortableType CSRF FormFactory createNamed Builder
  112. Thank you to Symfony ❤ Content credits - Icons from

    Freepik - Flaticon - Code rendered with carbon.now.sh Voilà !