oro meetups - symfony forms

16
Symfony Forms Ilya Antipenko [email protected] https ://github.com/aivus

Upload: ilya-antipenko

Post on 12-Apr-2017

152 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: ORO Meetups - Symfony Forms

Symfony FormsIlya Antipenko

[email protected]://github.com/aivus

Page 2: ORO Meetups - Symfony Forms

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

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

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

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

FORM EVENTS• Customizing Form based on the Underlying Data

WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016

Page 7: ORO Meetups - Symfony Forms

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

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

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

DATA TRANSFORMERS

WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016

Page 11: ORO Meetups - Symfony Forms

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

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

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

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

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

QUESTIONS?

Ilya [email protected]://github.com/aivus

WWW.GROSSUM.COM ILYA ANTIPENKO / ORO MEETUP 2016