I have looked all over and have found limited answers and maybe because its so obvious that its never needed to be asked.
I am trying to create a form. The purpose of this form is to allow members to recommend others to the site. You need to fill out first_name, last_name, and email. The problem is I want them to be able to send multiple emails at a time. So I probably would have another button that says "add another" and then it duplicates the form (first_name, last_name, and email).
I have looked at collections and I thought this was a great option because it allows you to allow_add and allows you to prototype but I am not sure how to make it work. I also considered embedding but I am not looking for an immediate response or validation so I don't think this is the solution.
I know javascript is going to be utilized to make this work.
What I have so far... Let me know if I am close.
This is where it is called.
public function referralFormAction(Request $request, $hash)
{
if (isset($hash)) {
$referral = new Referrals();
$referralForm = $this->createFormBuilder($referral)
//I know that 'email' needs to be replaced with something different, an object?
->add('email', 'collection', array(
'required' => false,
'allow_add' => true,
'prototype' => true,
//Not sure if I can set type like this.
'type' => new ReferralType(),
))
->getForm();
$referralForm->handleRequest($request);
if ($referralForm->isValid()) {
//do something
}
return $this->render('FuelFormBundle:Default:referralForm.html.twig', array('referralForm' => $referralForm->createView()));
} else {
throw $this->createNotFoundException('The product does not exist');
}
}
This is the Type
class ReferralType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add('first_name', 'text', array());
$builder->add('last_name', 'text', array());
$builder->add('email', 'email', array());
$builder->add('Send', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Fuel\FormBundle\Entity\Referrals',
));
}
public function getName()
{
return 'referral';
}
The answer to this is pretty sad. What you see above in the question is correct however I didn't populate any data in the form so there is nothing to show. So moral of the story is you either need to populate data so it will show something or add the JavaScript to finish the function so it will populate the text boxes.
I did find this line initially in the Symfony2 Reference for Collections
In both cases, no input fields would render unless your emails data array already contained some emails.
But I disregarded it because I understood it to mean something different at the time.
I did find this which was a huge help https://groups.google.com/forum/#!topic/symfony2/DjwwzOfUIuQ
And when I couldn't figure out my problem I messaged him and he responded with another helpful tidbit of info.
$form = $this->createFormBuilder()
->add('foo', 'foo_type', array(
'data_class' => 'Foo',
)
->add('bar', 'bar_type', array(
'data_class' => 'Bar',
)
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$foo = $form->get('foo')->getData();
$bar = $form->get('bar')->getData();
// do stuff
}
Big thanks to Bernhard for the help!
This documentation about How to Embed a Collection of Forms will teach you how to do exactly what you need; it is pretty self-explanatory and very well explained.