-
-
Save oste/5424292 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) | |
{ | |
if($event->getData() instanceof Comment){ | |
if($event->getData()->getThread()->getId() !== 'test') | |
$event->getForm()->remove('rating'); | |
} | |
} | |
public static function getSubscribedEvents() | |
{ | |
return array( | |
FormEvents::PRE_SET_DATA => array('form') | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fos_comment: | |
form: | |
comment: | |
type: app_comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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