oro meetups - symfony forms
TRANSCRIPT
![Page 2: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/2.jpg)
AGENDA• Basic usage• Form Events• Data Transformers• Extensions• Inheritance• Form rendering
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 3: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/3.jpg)
BASIC USAGE• Entity• Form type for entity• submit/handleRequest• isValid• ???• Done!
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 4: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/4.jpg)
BASIC USAGE• Each form contains methods, which can be overridden:
• buildForm - You can customize form in this method
• buildView - You can pass some additional variables to view representation
• finishView - like a buildView, but would be called after buildView in all children
• configureOptions - Configure form type. You can set required and defined options, add normalizers, etc.
• getBlockPrefix - generate form type prefix name for using in twig
• getParent - return parent form type
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 5: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/5.jpg)
FORM EVENTS• PRE_SET_DATA, POST_SET_DATA - Form::setData/form initialization• PRE_SUBMIT - Before new data will be applied, data - submitted data• SUBMIT - Form submitted, normalized data representation • POST_SUBMIT - Form submitted, view representation
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 6: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/6.jpg)
FORM EVENTS• Customizing Form based on the Underlying Data
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 7: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/7.jpg)
FORM EVENTS• Generate Form based on user Data (dependencies on current user)
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 8: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/8.jpg)
FORM EVENTS• Pay attention, events *_SET_DATA be called only for Form::setData().• When you call Form::submit() or Form::handleRequest() events
*_SET_DATA wouldn’t be called.• Use *_SUBMIT events to do some logic in Form::submit() or
Form::handleRequest() calls.
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 9: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/9.jpg)
DATA TRANSFORMERS• Model Data - Object which you pass to Form::setData()• Norm Data - intermediately state between Model and View Data
• View Data - view state of Object
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 10: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/10.jpg)
DATA TRANSFORMERS
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 11: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/11.jpg)
EXTENSIONS• Extensions allow to add additional logic to existed forms.• You must extend your extension class from AbstractTypeExtension
and implement method getExtendedType().• getExtendedType returns FQCN of extendable type.
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 12: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/12.jpg)
INHERITANCE• 2 ways to inherit forms:
• PHP Inheritance• Standard form inheritance (override getParent)
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 13: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/13.jpg)
FORM RENDERING• If you want to customise form type representation you can create a
special twig block.• Symfony generates block prefix by FQCN, but you can specify it
manually by overriding getBlockPrefix() method in FormType class.
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 14: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/14.jpg)
FORM RENDERING• You can specify customisation for whole FormType or individual field
(by name)• Use block prefix (BP) + _widget to customise FormType• Use BP + _row to customize row representation.• Use _ + {field name} + _widget to customize individual field• To customise field in compound form use block name with parent
block name: _ + {parent name} + _ + {field name} + _widget.
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 15: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/15.jpg)
FORM RENDERING• Twig helpers:
• form(form) - render all form fields with start/end tags• form_widget(form) - render all form fields• form_row(form.name) - render one form field. Contains:
• form_label(form.name) - render field label• form_errors(form.name) - render field errors• form_widget(form.name) - render field
• You always can pass additional variables to form widget block by using second argument: form_widget(form.name, {'custom': 'options'})
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
![Page 16: ORO Meetups - Symfony Forms](https://reader031.vdocuments.mx/reader031/viewer/2022022413/58edf6621a28ab110f8b4593/html5/thumbnails/16.jpg)
QUESTIONS?
Ilya [email protected]://github.com/aivus
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016