我一直在尝试显示带有数据json的表单。我不明白我需要做什么,因为这个表单有几个带有TextType的输入字段?一个输入字段中有一个值json。
我知道如何在无组件形式的情况下查看json数组,但我想以Symfony 4
组件形式查看json。
这是json数组
[{"name":"PHP","image":"php.png"},{"name":"java","image":"java.png"},{"name":"postgresql","image":"postgresql.png"},{"name":"JS","image":"js.png"}]
这是我的代码:
Classs ProjectsController
class ProjectsController extends BaseAdminController {
public function createEntityFormBuilder($entity, $view)
{
$formBuilder = parent::createEntityFormBuilder($entity, $view);
$formBuilder
->add('title')
->add('description', TextareaType::class)
->add('technology', ProjectTechnologyType::class)
->add('type', ProjectType::class)
->add('url');
return $formBuilder;
}
}
Class ProjectTechnologyType
class ProjectTechnologyType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('image', ProjectJsonType::class);
$builder->get('image')
->addModelTransformer(new CallbackTransformer(
function ($technologyAsArray) {
return json_encode($technologyAsArray);
},
function ($technologyAsJson) {
return json_decode($technologyAsJson);
}
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => ProjectTechnology::class
));
}
}
Class ProjectJsonType
class ProjectJsonType extends AbstractType{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([]);
}
public function getParent()
{
return CollectionType::class;
}
}
树枝
{% block main %}
{% block entity_form %}
{{ form_row(form.title) }}
{{ form_row(form.description) }}
{{ form_row(form.type) }}
{{ form_row(form.technology) }}
{{ form_row(form.url) }}
{% endblock entity_form %}
{% endblock %}