burgov/key-value-form-bundle
Composer 安装命令:
composer require burgov/key-value-form-bundle
包简介
A form type for managing key-value pairs
README 文档
README
A form type for managing key-value pairs.
Installation
$ composer require burgov/key-value-form-bundle:@stable
Then add the bundle to your application:
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Burgov\Bundle\KeyValueFormBundle\BurgovKeyValueFormBundle(), // ... ); }
Usage
To add to your form, use the KeyValueType type:
use Burgov\Bundle\KeyValueFormBundle\Form\Type\KeyValueType; use Symfony\Component\Form\Extension\Core\Type\TextType; $builder->add('parameters', KeyValueType::class, array('value_type' => TextType::class)); // or $formFactory->create(KeyValueType::class, $data, array('value_type' => TextType::class));
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_optionsoptional options to the child defined invalue_typekey_typedefines which form type to use to render the key field (default is atextfield)key_optionsoptional options to the child defined inkey_typeallowed_keysif this option is provided, the key field (which is usually a simple text field) will change to achoicefield, and allow only those values you supplied in the this option.use_container_objectsee 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
{% block burgov_key_value_widget %}
{{- block('sonata_type_native_collection_widget') -}}
{% endblock %}
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:
class Model { public function addOption($key, $value) { $this->options[$key] = $value; } }
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:
/** @var $builder Symfony\Component\Form\FormBuilderInterface */ $builder->add('options', 'burgov_key_value', array( 'required' => false, 'value_type' => TextType::class, 'use_container_object' => true, ));
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:
class Model { /** * Set the options. * * @param array|KeyValueContainer|\Traversable $data Something that can be converted to an array. */ public function setOptions($options) { $this->options = $this->convertToArray($options); } /** * Extract an array out of $data or throw an exception if not possible. * * @param array|KeyValueContainer|\Traversable $data Something that can be converted to an array. * * @return array Native array representation of $data * * @throws InvalidArgumentException If $data can not be converted to an array. */ private function convertToArray($data) { if (is_array($data)) { return $data; } if ($data instanceof KeyValueContainer) { return $data->toArray(); } if ($data instanceof \Traversable) { return iterator_to_array($data); } throw new InvalidArgumentException(sprintf('Expected array, Traversable or KeyValueContainer, got "%s"', is_object($data) ? getclass($data) : get_type($data))); } }
burgov/key-value-form-bundle 适用场景与选型建议
burgov/key-value-form-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 586.84k 次下载、GitHub Stars 达 44, 最近一次更新时间为 2014 年 01 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 burgov/key-value-form-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 burgov/key-value-form-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 586.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 44
- 点击次数: 29
- 依赖项目数: 14
- 推荐数: 3
其他信息
- 授权协议: MIT
- 更新时间: 2014-01-19