fjogeleit / dynamic-form-bundle
此包已被废弃,不再维护。未建议替代包。
DynamicForm创建数据库驱动的表单并持久化结果
0.9.1
2016-11-15 00:00 UTC
Requires
- php: >=7.1.3
- doctrine/doctrine-bundle: ~1.4
- doctrine/doctrine-fixtures-bundle: ^2.3
- doctrine/orm: ^2.5
- hautelook/alice-bundle: ^2.3
- sensio/framework-extra-bundle: ~5.2
- sonata-project/admin-bundle: ~3.0
- symfony/symfony: ~4.1
Requires (Dev)
- tm/tooly-composer-script: ^1.0
- dev-master
- 0.9.1
- 0.9.0
- 0.8.0
- 0.7.0
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.0
- 0.4.0
- 0.3.19
- 0.3.18
- 0.3.17
- 0.3.16
- 0.3.15
- 0.3.14
- 0.3.13
- 0.3.11
- 0.3.10
- 0.3.9
- 0.3.8
- 0.3.7
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.19
- 0.2.18
- 0.2.17
- 0.2.16
- 0.2.15
- 0.2.14
- 0.2.13
- 0.2.12
- 0.2.11
- 0.2.10
- 0.2.9
- 0.2.8
- 0.2.7
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.1
- 0.2.0
- 0.1.16
- 0.1.15
- 0.1.13
- 0.1.12
- 0.1.11
- 0.1.10
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-update-symfony-4
This package is auto-updated.
Last update: 2020-09-11 11:13:50 UTC
README
一个用于创建数据库驱动的动态表单并将其保存的Symfony包。
- 使用如SonataAdmin Bundle之类的后端创建表单
- 使用动态文本元素(如标题或文本)提高表单易用性
- 支持不同的表单字段,如输入、文本区域、复选框组、单选按钮组、文件输入
- 灵活可扩展,可通过自定义FormTypes和FormType配置进行扩展
- 可以集成子表单并将值保存为自定义ORM实体
需求
- PHP >= 5.6
- Composer
通过Composer安装
composer require fjogeleit/dynamic-form-bundle
配置
dynamic_form:
file_upload_dir: 'uploads' # upload directory for FileValues with web/ as root-dir
form_field: # disable field options to configure
disable_options:
- 'disabled'
- 'label'
- 'placeholder'
- 'required'
后端路由
在您的项目路由中激活dynamic-form-bundle路由。
dynamic_form:
resource: "@DynamicFormBundle/Controller/"
type: annotation
prefix: "dynamic-form"
安装SonataAdminBundle
不同的ValueType支持
示例
- 字符串
- 日期时间
- 实体
- 简单的数组
- 选项
- 浮点数
- ...
集成自定义子表单
创建Symfony FormType
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class);
$builder->add('email', TextType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Contact::class,
'label' => false
]);
}
}
为新的子表单创建ConfigurationObject
此对象必须实现DynamicFormType "ConfigurationInterface"。
use AppBundle\FormType\ContactType;
use DynamicFormBundle\Entity\DynamicResult\ResultValue\EntityValue;
use DynamicFormBundle\Services\FormType\ConfigurationInterface;
use DynamicFormBundle\Statics\FormFieldOptions\BaseOptions;
class ContactConfiguration implements ConfigurationInterface
{
/**
* @return string
*/
public function getName()
{
return 'contact';
}
/**
* @return string
*/
public function getFormTypeClass()
{
return ContactType::class;
}
/**
* @return string
*/
public function getValueClass()
{
return EntityValue::class;
}
/**
* @return array
*/
public function getAvailableOptions()
{
return BaseOptions::all();
}
}
它定义了
- 字段的后端命名
- 应在前端渲染的新FormType
- 将保存哪种类型的值
-
- 我们的ContactType将其值保存为Contact-Entity(data_class),因此其值类型是EntityValue
- 可以为此字段配置的选项数组,如必填。
至少配置ContactConfiguration为服务,使用TagName: "form.type_configuration"
services:
app.dynamic_form_type.contact.configuration:
class: 'AppBundle\Services\DynamicFormType\Configuration\ContactConfiguration'
tags:
- { name: 'form.type_configuration' }
现在新的Contact子表单作为新的FormField在后台可用。
EntityValue
为了将自定义实体作为DynamicForm子表单的"data_class",必须扩展DynamicForm:AbstractEntity类。
use Doctrine\ORM\Mapping as ORM;
use DynamicFormBundle\Entity\AbstractEntity;
/**
* @ORM\Entity
* @ORM\Table(name="contact")
*
* @package DynamicFormBundle\Tests\Functional\Fixtures\Entity
*/
class Contact extends AbstractEntity
{
/**
* @var string
*
* @ORM\Column(type="string")
*/
private $name;
/**
* @var string
*
* @ORM\Column(type="string")
*/
private $email;
/**
* @param string $name
* @param string $email
*/
public function __construct($name = null, $email = null)
{
$this->name = $name;
$this->email = $email;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @param string $email
*
* @return $this
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
}