I need to create a custom form but used within Symfony so I, therefore, have to create a Symfony form. However, I'd like to be able to use the functionality Sonta field types provide like ModeListType::class
.
So far I have created a custom route on my admin, within the controller action I create a new Symfony form. The controller action then returns a view with the form which extends the Sonata base edit layout.
class ExampleController
{
public function exampleAction(Request $request)
{
$order = new FooBar();
$modelManager = $this->get('sonata.admin.manager.orm');
$form = $this->createForm(ExampleType::class, $order, [
'model_manager' => $modelManager,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//
}
return $this->renderWithExtraParams('admin/test.html.twig', [
'form' => $form->createView(),
'action' => 'create',
'object' => $order,
'objectId' => null,
]);
}
}
My form:
class ExampleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('job', ModelListType::class, [
'model_manager' => $options['model_manager'],
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => FooBar::class,
]);
$resolver->setRequired([
'model_manager',
]);
}
}
And my template:
{% extends 'bundles/SonataAdminBundle/CRUD/base_edit.html.twig' %}
{% import "@SonataAdmin/CRUD/base_edit_form_macro.html.twig" as
form_helper %}
{% block title %}
Here
{% endblock %}
{% block sonata_tab_content %}
<div class="col-md-12">
<div class="row">
<div class="col-md-12">
<div class="box box-primary">
<div class="box-body">
{{ form(form) }}
</div>
</div>
</div>
</div>
</div>
{% endblock %}
This renders an input without the additional buttons like: List, Add, Delete. Clicking into the input also doesn't do anything, so it isn't treated as a Sonata input.
Any help on solving this would be great.