Download the PHP package burgov/key-value-form-bundle without Composer
On this page you can find all versions of the php package burgov/key-value-form-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download burgov/key-value-form-bundle
More information about burgov/key-value-form-bundle
Files in burgov/key-value-form-bundle
Package key-value-form-bundle
Short Description A form type for managing key-value pairs
License MIT
Informations about the package key-value-form-bundle
KeyValueFormBundle
A form type for managing key-value pairs.
Installation
Then add the bundle to your application:
Usage
To add to your form, use the KeyValueType
type:
The type extends the collection type, so for rendering it in the browser, the same logic is used. See the Symfony docs on collection types for an example on how to render it client side.
The type adds four options to the collection type options, of which one is required:
value_type
(required) defines which form type to use to render the value fieldvalue_options
optional options to the child defined invalue_type
key_type
defines which form type to use to render the key field (default is atext
field)key_options
optional options to the child defined inkey_type
allowed_keys
if this option is provided, the key field (which is usually a simple text field) will change to achoice
field, and allow only those values you supplied in the this option.use_container_object
see explanation below at 'The KeyValueCollection'
Besides that, this type overrides some defaults of the collection type and it's recommended you don't change them:
type
is set to BurgovKeyValueRow::class
and allow_add
and allow_delete
are always true
.
Working with SonataAdminBundle
In order to render your form with add/remove buttons, you need to extend the template SonataAdminBundle:Form:form_admin_fields.html.twig
and add this little piece of code
The KeyValueCollection
To work with collections and the Symfony2 form layer, you can provide an adder and a remover method. This however only works if the adder method expects one argument only. This bundle provides composed key-value pairs.
Your model class typically will provide a method like:
This will lead to the add method not being found by the form layer. To work
around this problem, the bundle provides the KeyValueCollection object. To use
it, you need to set the use_container_object
option on the form type to
true
. A form definition could look like this:
Your model class then needs to provide a setOptions
method that accepts a
Burgov\Bundle\KeyValueFormBundle\KeyValueContainer
argument. A flexible
implementation might look like this: