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

Symfony Forms - Advanced Use Cases

Symfony Forms - Advanced Use Cases

Alexandre Salomé

June 17, 2021
Tweet

More Decks by Alexandre Salomé

Other Decks in Technology

Transcript

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

    #3: Sortable fields #4: Security based fields
  2. 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
  3. Creating a form in the controller Configure the name of

    the generated form by using the FormFactory service.
  4. Creating a form type (1/2) You can also create a

    dedicated form class type, as explained in documentation.
  5. Login Form - Conclusion - Form manipulation - Type, data

    and options - Form tree - Type hierarchy
  6. ChangePasswordType Data is a User object Children - Old password

    - New password repeated Dependency to UserPasswordEncoder
  7. 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
  8. 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
  9. 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
  10. Change password - Conclusion - No change in controller -

    Events & Subscribers - Lifecycle - Relation to the model
  11. Form rendering ERRORS LABEL LABEL LABEL WIDGET WIDGET LABEL WIDGET

    WIDGET WIDGET LABEL LABEL ROW ROW ROW ROW ROW ROW
  12. Form rendering ROW ERRORS LABEL ROW WIDGET ERRORS LABEL ROW

    WIDGET ERRORS LABEL ROW WIDGET ERRORS LABEL
  13. Sortable - Conclusion - Form templating engine - No change

    in page template - Form view model (row/label/widget/error) - Block name resolution logic
  14. 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
  15. Thank you to Symfony ❤ Content credits - Icons from

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