Form/Type
class DemoType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text');
}
// ...
}
Controller
$user = new User();
$form = $this->createForm(new DemoType(), $user);
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->get('doctrine')->getEntityManager();
$em->persist($user);
$em->flush();
return // ...
}
}
View
<form action="{{ path }}" method="post" {{ form_enctype(form) }}>
<div>
{{ form_widget(form.name) }}
</div>
{{ form_rest(form) }}
</form>
I would like to register plurality at once.
(Finally I would like to add an input field by the view side.(plus button etc. JavaScript))
What should I do to register two or more users at once?
You should be using the collection form type. See here for a detailed explanation. The plus button and dynamic creation of new subform has to be handled by javascript but it’s all explained in the documentation.