Last active
August 29, 2015 14:11
-
-
Save JuanLuisGarciaBorrego/4b0313fdbeea8bc4b234 to your computer and use it in GitHub Desktop.
Form/Type/TaskTypeEvent.php
This file contains 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 JuanLuis\LabBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
/* | |
* La Clase FormEvent es una clase que extiende el EventDispatcher | |
* | |
* La Clase FormEvents contiene los diferentes eventos que pueden | |
* ser lanzados usando formularios | |
*/ | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
class TaskType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){ | |
$task = $event->getData(); | |
$form = $event->getForm(); | |
//Comprobamos que el objecto Task es nuevo | |
//Y En caso de que sea nuevo añadimos el campo name | |
if(!$task || null === $task->getId()){ | |
$form->add('name', 'text', | |
array( | |
'max_length' => 100 | |
) | |
); | |
} | |
}); | |
$builder | |
->add('description'); | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'JuanLuis\LabBundle\Entity\Task' | |
)); | |
} | |
public function getName() | |
{ | |
return 'Task'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment