Skip to content

Instantly share code, notes, and snippets.

@mikaelcom mikaelcom/Controller.php
Last active Jul 31, 2017

Embed
What would you like to do?
<?php
class SomeClass
{
public function someMethod()
{
$parametersFieldsForm = $form->create('parameters', null, [
'compound' => true,
'translation_domain' => false,
]);
foreach ($parameters as $parameter) {
$this->addFieldsFromParameter($parametersFieldsForm, $parameter, $parameter->getName());
}
$form->add($parametersFieldsForm);
}
protected function addFieldsFromParameter(FormBuilder $form, AbstractGeneratedStruct $struct, $baseFormName)
{
/** @var StructFieldGenerator $structFieldGenerator */
$structFieldGenerator = $this->get('providr.struct_field_generator');
/**
* form part fields to be generated
*/
if (strpos($baseFormName, '[')) {
$formBaseNameParts = explode('[', $baseFormName);
$fieldsForm = null;
foreach ($formBaseNameParts as $index => $formBaseNamePart) {
// first item is form, this is the default first form level that we don't care
if ($index > 0) {
if (isset($fieldsForm)) {
$form->add($fieldsForm);
$form = $fieldsForm;
}
$fieldsForm = $form->create(str_replace(']', '', $formBaseNamePart), null, [
'compound' => true,
'label_attr' => [
'class' => 'hidden',
],
'csrf_protection' => false,
'translation_domain' => false,
]);
}
}
foreach ($struct->getProperties() as $property) {
$structFieldGenerator->addStructToForm($fieldsForm, $property, $this);
}
$form->add($fieldsForm);
} else {
$structFieldGenerator->addStructToForm($form, $struct, $this);
}
}
/**
* @Route("/formpart", name="admin_formpart", condition="request.isXmlHttpRequest()")
* @Method({"GET"})
*/
public function viewExecutionFormPartAction(Request $request)
{
$form = $this->createFormBuilder(null, [
'csrf_protection' => false,
]);
/** @var AbstractGeneratedStruct $struct */
$this->addFieldsFromParameter($form, $struct, $request->query->get('field_name'));
return new Response($this->renderView('...'), [
'form' => $form->getForm()->createView(),
]);
}
}
<?php
class StructFieldGenerator
{
/**
* @param FormBuilder $form
* @param AbstractGeneratedStruct $struct
* @param Controller $controller
*/
public function addStructToForm(FormBuilder $form, AbstractGeneratedStruct $struct, Controller $controller)
{
$form->add($struct->getName(), $this->getFieldType($struct), $this->getFieldOptions($struct, $controller));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.