Symfony 3 - 来自数据库的动态表单字段

时间:2016-11-02 10:39:21

标签: php mysql symfony

我在 fields 的数据库中有一个表: - field_label,field_type,field_case等

我想通过field_case以字形方式向表单添加字段。

例如: 在此表单类型中,我想使用field_case = 1

添加数据库中的所有字段
Vector2 ray = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray, Vector2.zero);
if (hit)
{
    flag = true;
    endPoint = hit.point;
    endPoint.y = yAxis;
    Debug.Log(endPoint);
}

我想从表字段添加其中field_case = 1

public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add("companie_uid", HiddenType::class); $builder->add("companie_denumire", TextType::class, array('label' => 'companie_denumire')); $builder->add("companie_cui", TextType::class, array('label' => 'companie_cui', 'required' => false)); $builder->add("companie_j", TextType::class, array('label' => 'companie_j', 'required' => false)); $builder->add("companie_mail", EmailType::class, array('label' => 'companie_mail', 'required' => false)); $builder->add("companie_website", TextType::class, array('label' => 'companie_website', 'required' => false)); $builder->add("companie_status", HiddenType::class); $builder->add("companie_descriere", TextAreaType::class, array('label' => 'companie_descriere', 'required' => false)); $builder->add("companie_telefon", TextType::class, array('label' => 'companie_telefon', 'required' => false)); $builder->add("companie_iban", TextType::class, array('label' => 'companie_iban', 'required' => false)); $builder->add("companie_banca", TextType::class, array('label' => 'companie_banca', 'required' => false)); $builder->add("file", FileType::class, array('label' => 'companie_file', 'mapped' => false, 'required' => false)); $builder->add("save", SubmitType::class, array('label' => 'companie_save')); $builder->add( $builder->create('address', CompanyAddressType::class, Array('by_reference' => false,)) ); }

并将它们保存到另一个表 field_values 中的数据库。

1 个答案:

答案 0 :(得分:0)

您可以使用form listener

$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
    $data = $event->getData();

    switch($data->getFieldCase())
    {
        case 1:
        $builder->add(field_id, field_type, array('label' => 'field_label'));
        break;
        // ...
    }    
});
相关问题