Reputation: 191
I have a many to many relation between two entities.
I display then a form to add entityA to entityB. Isn't possible to add a customized form (I mean in the twig view) in order to enable the user sometimes to select one value and sometimes more than one?
when I want the user to select more than one value, I use this
<select multiple>
{% for entity in entitys %}
<option>
{{entity.id}}
</option>
{%endfor%}
</select>
otherwise this
<select >
{% for entity in entitys %}
<option>
{{entity.id}}
</option>
{%endfor%}
</select>
but now the problem is how to submit the form. the
<button type="submit" class="btn btn-info" value="NEXT STEP " />
Here is the whole form
<form method="post">
<select >
{% for entity in entitys %}
<option>
{{entity.id}}
</option>
{%endfor%}
</select>
<input type="submit" />
</form>
no longer submit the form. any ideas plz??
here is my entire twig view
<h2> STEP {{step}} </h2>
<form method="post">
<select >
{% for entity in entitys %}
<option value="{{entity.id}}">
{{entity.id}}
</option>
{%endfor%}
</select>
<input type="submit" class="btn btn-info" />
</form>
<br>
<br>
Upvotes: 0
Views: 2093
Reputation: 3458
In your formbuilder you can add some options like in my exemple:
The aim is to map your field to an entity (to setup the list). Don't forget to add a method _tostring to your mapped entity to make symfony able to represent your entity as a text in your select.
public function buildForm(FormBuilder $builder, array $options) {
$id = $this->id;
$builder->add(
'addressees',
'entity',
array(
'class' => 'Pref27\MailBundle\Entity\Addressee',
'property' => 'name',
'multiple' => true,
'expanded' => false,
'required' => true,
'label' => 'mail.add.theme';
}
)
);
}
$editForm = $this->createForm(new FormType(), $entity);
return array(
'form' => $editForm->createView()
);
<form action="{{ path('theControllerActionWitchIsResponsibeOfRecordingIntoDatabase' }}" method="post" {{ form_enctype(edit_form) }}>
{{ form_widget(edit_form) }}
<p>
<button type="submit">Next step</button>
</p>
</form>
The type of field rendered will depend on setting of multiple and expended
select tag expanded=false multiple=false
select tag (with multiple attribute) expanded=false multiple=true
radio buttons expanded=true multiple=false
checkboxes expanded=true multiple=true
you can find more information about entity type in form here : http://symfony.com/doc/2.0/reference/forms/types/entity.html
From your twig view form's action is missing try to add
<form method="post" action="{{ path("theRouteOfYourControllerWitchRecordTheData")}}">
don't forget to add {{form_rest(form) }}
To tell twig to add the CSRF token
and don't forget to add value in your select's option
<select multiple>
{% for entity in entitys %}
<option value="{{entity.id}}">{{entity.name}}</option>
{%endfor%}
</select>
Upvotes: 2