Skip to content

Instantly share code, notes, and snippets.

@stof
Forked from oste/CommentFormSubscriber.php
Last active December 16, 2015 10:38
Show Gist options
  • Save stof/5421050 to your computer and use it in GitHub Desktop.
Save stof/5421050 to your computer and use it in GitHub Desktop.
<?php
namespace CommentBundle\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
class CommentFormSubscriber implements EventSubscriberInterface
{
public function form(FormEvent $event)
{
$comment = $event->getData();
if($comment->getThread()->getId() == 'test')
$event->getForm()->remove('rating');
}
public static function getSubscribedEvents()
{
return array(
FormEvents::PRE_SET_DATA => array('form')
);
}
}
<?php
class CommentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('rating', 'choice', array(
'choices' => array(
'5' => '5',
'4' => '4',
'3' => '3',
'2' => '2',
'1' => '1',
),
'multiple' => false,
'expanded' => true
));
$builder->addEventSubscriber(new CommentFormSubscriber());
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'App\Entity\Comment',
));
}
public function getParent()
{
return 'fos_comment_comment';
}
public function getName()
{
return "app_comment";
}
}
fos_comment:
form:
comment:
type: app_comment
services:
app_comment.form_type.comment:
class: App\Form\CommentType
tags: [{ name: form.type, alias: app_comment }]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment