Skip to content

Instantly share code, notes, and snippets.

@thoroc
Last active December 18, 2015 08:39
Show Gist options
  • Save thoroc/5756010 to your computer and use it in GitHub Desktop.
Save thoroc/5756010 to your computer and use it in GitHub Desktop.
<?php
namespace Acme\AuditBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Acme\AuditBundle\Entity\AuditFormField;
use Acme\AuditBundle\Entity\AuditFormSection;
use Acme\AuditBundle\Form\Type\AuditFormFieldType;
class AuditFormFieldController extends Controller
{
...
/**
* Edit existing field or create new field
*
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return twig template
*/
public function editAction( Request $request )
{
...
$field = new AuditFormField();
$form = $this->createForm( new AuditFormFieldType(), $field, array(
'section' => $field->getSection(),
));
...
return $this->render( 'AcmeAuditBundle:AuditFormField:edit.html.twig', array(
'field' => $field,
'form' => $form->createView(),
));
}
...
}
<?php
namespace Acme\AuditBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Acme\AuditBundle\Entity\AuditScore;
use Acme\AuditBundle\Entity\AuditFormField;
class AuditFormFieldType extends AbstractType
{
...
public function buildForm( FormBuilderInterface $builder, array $options )
{
...
$builder->add( 'section', 'audit_section', array(
'data' => ( isset($options['section']) ) ? $options['section']->getId() : null,
));
...
}
...
public function setDefaultOptions( OptionsResolverInterface $resolver )
{
$resolver->setDefaults( array(
'data_class' => 'Acme\AuditBundle\Entity\AuditFormField',
'section' => null,
));
}
}
<?php
namespace Acme\AuditBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\Common\Persistence\ObjectManager;
class SectionType extends AbstractType
{
private $repository;
public function __construct( ObjectManager $objectManager )
{
$this->repository = $objectManager->getRepository( 'AcmeAuditBundle:AuditFormSection' );
}
public function setDefaultOptions( OptionsResolverInterface $resolver )
{
$type = $this;
$choiceList = function( Options $options ) use ( $type )
{
return new SimpleChoiceList( $type->getSections( $options['auditform']) );
};
$resolver->setDefaults( array(
'choice_list' => $choiceList,
'virtual' => true,
'empty_value' => '(Select Section)',
'empty_data' => null,
'auditform' => null,
));
}
public function getSections( $auditform = null )
{
$array = array();
foreach( $this->repository->getSections( $auditform ) as $set )
{
if( !$set->getAuditForm() ) { continue; }
if( !array_key_exists( $set->getAuditForm()->getTitle(), $array ))
{
$array[$set->getAuditForm()->getTitle()] = array();
}
$array[$set->getAuditForm()->getTitle()][$set->getId()] = $set;
}
return $array;
}
public function getParent()
{
return 'choice';
}
public function getName()
{
return 'audit_section';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment