Skip to content

Latest commit

 

History

History
42 lines (36 loc) · 3.28 KB

custom_form_options.md

File metadata and controls

42 lines (36 loc) · 3.28 KB

Modify the options array for building the form

The form that is constructed for creating and updating entities always requires a few options. By default the BravesheepCrudifyBundle provides the method and action properties, which are required by forms to indicate by which method they should be submitted back to the server and what url they should be submitted to. These properties are determined by a Bravesheep\CrudifyBundle\Form\OptionsProvider\OptionsInterface instance. Specifically the BravesheepCrudifyBundle uses Bravesheep\CrudifyBundle\Form\OptionsProvider\BasicOptions if you have not specified another form options provider yourself.

Note that all options providers should always return at least a method and action property. The best way to do this is by extending the BasicOptions provider provided by the BravesheepCrudifyBundle and calling the parent methods on your own implementation.

Note that using options is never the correct way to provide Dependency Injection in your forms. If you need to inject some service in your form, you should instead make your form a service and inject some service there instead. Typically you can say that if the options you are providing to your form are not some sort of scalar value (i.e. integers, floats, booleans, strings) then it should not be provided as an option to your form.

Also it is not appropriate to modify entities inside the FormType. Use a object retriever to load the correct entity directly. An exception might be made for creating a new instance of an object when none is provided for the newAction and createAction. However then you may want to use the empty_data option that Symfony2 forms provide by default. Used in combination with the POST_SUBMIT to update inverse sides of non-owning-side entities, you should be able to fully get an updated object from the form component.

Implementable methods

The Form Options provider has two methods available. One method, OptionsInterface::getCreateOptions(), is used for retrieving the options for the create form. Note that this method only uses the controller and definition. The second method is OptionsInterface::getUpdateOptions() also takes the controller and definition, but additionally gets provided with the current object on which the form should be created. You can use this object to dynamically modify the options to be provided to your update form based on the properties of the object.

Updating the mapping

In order to activate the custom form options provider for a single mapping, you need to update the mapping.form_options_provider key with a service that contains your custom form options provider. You can then use Dependency Injection on that service. Alternatively, if you do not require any Dependency Injection, you can use the name of the class directly. The class will need to be constructable without having any required parameters in this case however. Also take a look at the default config.