oro meetups - symfony forms
TRANSCRIPT
Symfony FormsIlya Antipenko
[email protected]://github.com/aivus
AGENDA• Basic usage• Form Events• Data Transformers• Extensions• Inheritance• Form rendering
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
BASIC USAGE• Entity• Form type for entity• submit/handleRequest• isValid• ???• Done!
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
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
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
FORM EVENTS• Customizing Form based on the Underlying Data
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
FORM EVENTS• Generate Form based on user Data (dependencies on current user)
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
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
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
DATA TRANSFORMERS
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
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
INHERITANCE• 2 ways to inherit forms:
• PHP Inheritance• Standard form inheritance (override getParent)
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016
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
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
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
QUESTIONS?
Ilya [email protected]://github.com/aivus
WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016