Symfony Embedded Forms ‘take 2’
Learnt some new things while working with symfony’s embedded forms, as we know, when they are have relations in the entities that the form is modelled after you want to ensure that when including the other form type that you create an instance of the formType representing the many in the manyToOne relationship. For example in the forum i am working on (which you can find on github here though currently still in development as of the date of this blog entry) a Topic has MANY Posts, as a result, we build our form by instantiating the PostType first and then adding the TopicType to it as a field because the PostType has a related Topic in the database schema.
But following up from the last post, what about when we have unique use cases for our form Types? What if we want to use a PostType without including the TopicType or we wish to conditionally decide wether for the purposes of editing we need to populate the form before presenting it?
To make this easier, in my FormHandlers what i choose to do is pass an array with some options we can use, namely a mode key specifies wether we are to ‘insert’ or ‘update’ an entity, and other keys for holding the entity to populate the form if mode is ‘update’
Here is an example from the top half of my TopicFormHandler:
class TopicFormHandler { protected $factory; protected $request; protected $options; protected $form; public function __construct($factory, Request $request, array $options = null ) { $this->factory = $factory; $this->request = $request; $this->options = $options; if ($this->options['mode'] == 'update') { $this->form = $factory->create(new PostType(), $this->options['post']); $this->form->add($factory->create(new TopicType(), $this->options['post']->getTopic())); } else { $this->form = $factory->create(new PostType()); $this->form->add($factory->create(new TopicType())); } } }
To view the full source in greater context, explore the source on github here.
interesting development, encouragements, have you thought about joining phpbb4?
@Cordoval thanks, but phpBB does not really interest me. I heard they were building the next version of phpBB on Symfony somewhere before.
I much prefer writing my own project code though, large community projects suffer from issues of bureaucracy like decision by committee and thats not always the most productive way in my view. Sometimes its not the best ideas that make it through but the ideas that are most supported in that community. Not saying my ideas are the best, just rather do things my own way. If i make mistakes along the way then i am happy to fix them and learn from them.
The forum i am building is coming along nicely though, far from perfect but i have enjoyed the experience of building my own. It needs a lot of work but the basics are down now so its a case of refining what i have now before adding to many more features. Basic browsing, topic reading, topic posting and replying is all in place and working now, i just need to add a couple more bits here and there and some kind of BB Code parser then i am set to go. 🙂
Thanks for commenting Cordoval.
Thank you for the auspicious writeup. It in reality was a enjoyment account it. Glance advanced to more introduced agreeable from you! However, how could we keep up a correspondence?