Reputation: 503
I have two entities, Registro and RegistroProfesional. One Registro has many RegistroProfesional. (OneToMany).
In the form, I want the first RegistroProfesional to appear, but it gives me the error "Entities passed to the choice field must be managed. Maybe they persist in the entity manager?"
This is my field in formtype:
$builder->add('registroProfesionales','collection', array(
'type'=> new RegistroProfesionalType(),
'cascade_validation' => true,
'allow_delete' => true,
'allow_add' => true,
'prototype' => true,
'prototype_name' => '__registro_profesionales__',
'label' => false
))
In my controller:
$entity = new Registro();
$registroProfesional = new RegistroProfesional();
$entity->addRegistroProfesionale($registroProfesional); // Here add the first RegistroProfesional
$flow = $this->get('biobanco.form.flow.crearRegistro');
$flow->bind($entity);
$form = $flow->createForm();
if ($flow->isValid($form)) {
$flow->saveCurrentStepData($form);
if ($flow->nextStep()) {
// form for the next step
$form = $flow->createForm();
} else {
//dump($form,$entity);die();
// flow finished
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
$flow->reset(); // remove step data from the session
$this->enviarCorreo($entity->getId(), 'REGISTRO');
return $this->render('BiobancoBundle:Registro:realizado.publico.html.twig');
}
}
View:
{% for rp in form.registroProfesionales %}
<div class="col-sm-6 col-xs-12">
<label>Nombre</label>
{{ form_widget(rp.nombre) }}
{% if form_errors(rp.nombre) is not empty %}
<div class="alert alert-danger alert_form alert-dismissible fade in" role="alert">
{{ form_errors(rp.nombre) }}
</div>
{% endif %}
</div>
<div class="col-sm-6 col-xs-12">
<label>Primer Apellido</label>
{{ form_widget(rp.apellido1) }}
{% if form_errors(rp.apellido1) is not empty %}
<div class="alert alert-danger alert_form alert-dismissible fade in" role="alert">
{{ form_errors(rp.apellido1) }}
</div>
{% endif %}
</div>
<div class="col-sm-6 col-xs-12">
<label>Segundo Apellido</label>
{{ form_widget(rp.apellido2) }}
{% if form_errors(rp.apellido2) is not empty %}
<div class="alert alert-danger alert_form alert-dismissible fade in" role="alert">
{{ form_errors(rp.apellido2) }}
</div>
{% endif %}
</div>
<div class="col-sm-6 col-xs-12">
<label>Email</label>
{{ form_widget(rp.email) }}
{% if form_errors(rp.email) is not empty %}
<div class="alert alert-danger alert_form alert-dismissible fade in" role="alert">
{{ form_errors(rp.email) }}
</div>
{% endif %}
</div>
{% endfor %}
Where is the problem?
Upvotes: 0
Views: 29
Reputation: 3051
The problem, most probably, is not in the code you've listed.
I would say you've got something in $form = $flow->createForm();
or in RegistroProfesionalType
itself. Apparently, you've got somewhere some kind of choice field (probably, EntityType
, i.e usual doctrine relation) and mapped on this field property has a value which is not yet persisted.
Upvotes: 1