Let's say we have an Event entity and we need two forms: an EventNew form to create a new Event and another form that we will call EventEdit in order to edit an Event entity. We want our New form to have the following fields:
- Name
- Category
- Subcategory
- Datetime
On the other hand, we want the EventEdit form to have the same fields except for Datetime (for some reason we don't want the user to be able to change the Datetime of the event once it's created). In addition, we have the Edit form to have a textarea field named "description" (we didn't want to annoy the user requiring a description for the event when it's created). So the list of fields for the edit form would be as follows:
- Name
- Category
- Subcategory
- Description
We have at least two ways to solve this problem. We can create a BaseEventType class including the common fields for both forms and then inherit from it adding/removing fields as needed or we can create just two forms (the "new" one and the "edit" one) and make the EventEditType inherit from EventNewType. If you're sure you won't need more Event forms in the future you can just stick to option two. If we take option one we should create a class named BaseEventType containing all the common fields:
Now we need an EventNewType for our new event form and we'll add the datetime field. Note that we need to call to the method buildForm of parent class (BaseEventType, which we extend).
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
namespace Devvness\EventBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
class BaseEventType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->add('name') | |
->add('category') | |
->add('subcategory'); | |
} | |
public function getName() | |
{ | |
return 'base_event'; | |
} | |
} |
Now we need an EventNewType for our new event form and we'll add the datetime field. Note that we need to call to the method buildForm of parent class (BaseEventType, which we extend).
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
namespace Devvness\EventBundle\Form\Type; | |
use Devvness\EventBundle\Form\Type\BaseEventType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
class EventNewType extends BaseEventType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
parent::buildForm($builder, $options); | |
$builder->add('datetime'); | |
} | |
public function getName() | |
{ | |
return 'event_new'; | |
} | |
} |
Last but not least we create our EventEditType and we already have our edit event form:
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
namespace Devvness\EventBundle\Form\Type; | |
use Devvness\EventBundle\Form\Type\BaseEventType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
class EventEditType extends BaseEventType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
parent::buildForm($builder, $options); | |
$builder->add('description', 'textarea'); | |
} | |
public function getName() | |
{ | |
return 'event_edit'; | |
} | |
} |
If we decided to take option 2 we should create a EventNewType including all 4 fields (name, category, subcategory, datetime). Then we would create EventEditType inheriting from EventNewType and add/remove fields as necessary:
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
class EventEditType extends EventNewType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
parent::buildForm($builder, $options); | |
$builder->remove('datetime') | |
->add('description'); | |
} | |
.... | |
} |
No hay comentarios:
Publicar un comentario