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

Symfony2 Forms: Past, Present, Future

Symfony2 Forms: Past, Present, Future

Held at the SymfonyCon 2013 in Warsaw, Poland.

The Symfony2 Form component is a critical part for many Symfony2 applications. As such, we have been constantly improving the component to handle the many different use cases of our users and to lower the barrier to entry for newcomers. As seasoned Symfony2 developer, all these changes can be sometimes hard to track. What about backwards compatibility? Will your business-critical applications break? And the questions don't stop there. How can you get up to scratch with the latest best practices? What can you expect from future versions? And how can you get involved to help things move faster? I will try to answer these and many other questions in my talk.

Bernhard Schussek

December 13, 2013
Tweet

More Decks by Bernhard Schussek

Other Decks in Programming

Transcript

  1. Bernhard Schussek @webmozart 1/89 Symfony2 Forms Symfony2 Forms Past, Present,

    Future Past, Present, Future Bernhard Bernhard Schussek aka @webmozart Schussek aka @webmozart SymfonyCon Warsaw 2013 SymfonyCon Warsaw 2013 http://www.flickr.com/photos/94334088@N03/11339593125/
  2. Bernhard Schussek @webmozart 2/89 Bernhard Schussek Not: Bernard Bernhart Bernardt

    Bernharth Also Wrong: Schusseck Schusek Shusseck Scussek Shcusek Schuseck Shusek Schuhsek Shuhseck Shushek Shuseck Shusec Shushek Shushek Shushec Sussek Shussec Schußeck Scusek Shußec Schuhsec Shußec Schushek Schusheck
  3. Bernhard Schussek @webmozart 7/89 flag of Austria not the flag

    of Austria http://en.wikipedia.org/wiki/File:Flag_of_Austria.svg http://en.wikipedia.org/wiki/File:Zeichen_267.svg
  4. Bernhard Schussek @webmozart 13/89 Form Growth 19/02/07 19/02/08 19/02/09 19/02/10

    19/02/11 19/02/12 19/02/13 0 5000 10000 15000 20000 25000 30000 35000 40000 0 5000 10000 15000 20000 25000 30000 35000 40000 NCLOC 2.0 1.1 1.4 2.1 2.2 2.4
  5. Bernhard Schussek @webmozart 22/89 Request Handling (<2.3) $form = $this->createForm('task',

    $task); if ($request->isMethod('POST')) { $form->bind($request); if ($form->isValid()) { ... } }
  6. Bernhard Schussek @webmozart 23/89 Request Handling (<2.3) <form action="{{ path('task_new')

    }}" method="post" {{ form_enctype(form) }}> {{ form_widget(form) }} <input type="submit" /> </form>
  7. Bernhard Schussek @webmozart 24/89 Request Handling (2.3+) $form = $this->createForm('task',

    $task, array( 'action' => $this->generateUrl('task_new'), )); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { ... }
  8. Bernhard Schussek @webmozart 25/89 Request Handling (2.3+) {{ form_start(form) }}

    {{ form_widget(form) }} <input type="submit" /> {{ form_end(form) }}
  9. Bernhard Schussek @webmozart 27/89 Non-POST Requests $form = $this->createForm('task', $task,

    array( 'action' => $this->generateUrl('task_new'), 'method' => 'PATCH', )); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { ... }
  10. Bernhard Schussek @webmozart 28/89 Fluid FormBuilder Interface $form = $this->createFormBuilder($task)

    ->setAction($this->generateUrl('task_new')) ->setMethod('PUT') ... ->getForm();
  11. Bernhard Schussek @webmozart 30/89 Dynamic Forms (2.3+) $addProvince = function

    (FormEvent $event) { $form = $event->getForm()->getParent(); $country = $event->getForm()->getData(); $form->add('province', 'entity', array( ... )); }; $builder->get('country')->addEventListener( FormEvents::POST_SET_DATA, $addProvince );
  12. Bernhard Schussek @webmozart 31/89 province POST_SET_DATA add('province', 'entity', ...) Dynamic

    Forms (2.3+) form country setData( ) object (Shop) object (Shop) object (Country) setData($shop->getCountry()) object (Province) setData($shop->getProvince()) POST_SET_DATA POST_SET_DATA
  13. Bernhard Schussek @webmozart 33/89 province province POST_SUBMIT add('province', 'entity', ...)

    Dynamic Forms (2.3+) form country submit(array(...)) submit("23") submit("17") POST_SUBMIT POST_SUBMIT object (Shop) object (Country) object (Province)
  14. Bernhard Schussek @webmozart 40/89 Creating a CSRF Token (2.3+) use

    Symfony\Component\Security\Csrf\CsrfTokenManager; $tokenManager = new CsrfTokenManager(); $token = $tokenManager->getToken('shoppingCart'); echo $url.'&token='.$token->getValue();
  15. Bernhard Schussek @webmozart 41/89 CSRF Token Verification (2.3+) use Symfony\Component\Security\Csrf\CsrfToken;

    $userToken = new CsrfToken( 'shoppingCart', $request->query->get('token') ); if (!$tokenManager->isTokenValid($userToken)) { throw new SecurityException('CSRF attack!'); }
  16. Bernhard Schussek @webmozart 44/89 Callback Constraint (<2.3) use Symfony\Component\Validator\ ExecutionContextInterface;

    public function validateTotalPrice( ExecutionContextInterface $context) { if ($this->totalPrice > $this->calculateTotal()) { $context->addViolationAt( 'totalPrice', 'The total price is incorrect.' ); } }
  17. Bernhard Schussek @webmozart 45/89 Callback Constraint (2.3+) class ShoppingCart {

    /** * @Assert\Callback */ public function validateTotalPrice($context) ... }
  18. Bernhard Schussek @webmozart 48/89 Claim 1 Claim 1 "Constant Change"

    "Constant Change" http://www.flickr.com/photos/nanagyei/6636632951/
  19. Bernhard Schussek @webmozart 49/89 19/02/07 19/02/08 19/02/09 19/02/10 19/02/11 19/02/12

    19/02/13 0 5000 10000 15000 20000 25000 30000 35000 40000 0 100 200 300 400 500 600 700 800 900 NCLOC Commit Count 2.0 1.1 1.4 2.1 2.2 2.4 Commit Count
  20. Bernhard Schussek @webmozart 51/89 class PropertyPathMapper implements DataMapperInterface { ...

    + /** + * Creates a new property path mapper. + * + * @param PropertyAccessorInterface $propertyAccessor + */ + public function __construct(PropertyAccessorInterface + $propertyAccessor = null) + { + $this->propertyAccessor = $propertyAccessor + ?: PropertyAccess::createPropertyAccessor(); + } ... }
  21. Bernhard Schussek @webmozart 52/89 class CustomMapper extends PropertyPathMapper { public

    function __construct() { $this->initialized = false; } } broken!
  22. Bernhard Schussek @webmozart 53/89 class CustomMapper extends PropertyPathMapper { public

    function __construct() { + parent::__construct(); $this->initialized = false; } }
  23. Bernhard Schussek @webmozart 56/89 2.0 2.1 2.2 2.3 2.4 0

    5 10 15 20 25 30 BC Breaks Deprecations 2.1 2.1 2.2 2.3 2.4 BC Breaks and Deprecations
  24. Bernhard Schussek @webmozart 57/89 Claim 2 Claim 2 "Basic Features

    Are Missing" "Basic Features Are Missing" http://www.flickr.com/photos/annagaycoan/3834802666/
  25. Bernhard Schussek @webmozart 62/89 The The Community Community Can Easily

    Add Features Can Easily Add Features http://www.flickr.com/photos/gulsenozcan/9323768024/
  26. Bernhard Schussek @webmozart 64/89 Features Features Are Are Missing… Missing…

    http://www.flickr.com/photos/31246066@N04/6365239995/
  27. Bernhard Schussek @webmozart 65/89 03/07/10 03/01/11 03/07/11 03/01/12 03/07/12 03/01/13

    03/07/13 0 50 100 150 200 250 Bug Enhancement Feature Unlabeled 2.1 2.2 2.3 2.4 2.0 Form Issues+PRs
  28. Bernhard Schussek @webmozart 66/89 We Need We Need Your Your

    Help! Help! http://www.flickr.com/photos/59949757@N06/10822072804
  29. Bernhard Schussek @webmozart 75/89 Claim 4 Claim 4 "No Community

    Support" "No Community Support" http://www.flickr.com/photos/jonathankosread/9283478903/
  30. Bernhard Schussek @webmozart 76/89 19/02/07 19/02/08 19/02/09 19/02/10 19/02/11 19/02/12

    19/02/13 0 100 200 300 400 500 600 700 800 900 0 10 20 30 40 50 60 70 80 90 100 Authors Commit Count 2.0 2.1 2.2 2.4 Number of Commit Authors
  31. Bernhard Schussek @webmozart 77/89 How To Get How To Get

    Involved Involved? ? http://www.flickr.com/photos/pagedooley/3258088498/
  32. Bernhard Schussek @webmozart 83/89 and if you have time… and

    if you have time… http://www.flickr.com/photos/see-through-the-eye-of-g/6268973351/
  33. Bernhard Schussek @webmozart 86/89 Actual Core Development reproduce #1234 not

    reproducible reproduce #5678 not fixable attempt fix reproduce #9123 implement fix
  34. Bernhard Schussek @webmozart 87/89 Ideal Core Development #5678 #7856 not

    fixable attempt fix #9123 implement fix implement feature attempt fix not fixable #4321
  35. Bernhard Schussek @webmozart 89/89 Questions? Questions? http://joind.in/talk/view/10362 http://joind.in/talk/view/10362 Thank you!

    Thank you! Bernhard Schussek Bernhard Schussek @webmozart @webmozart http://www.flickr.com/photos/59949757@N06/10822072804