I am currently facing a problem, I have a symfony form with twice the same entity inside. Two diplomas. For one of theme i have to modify one field and for the other 3 fields. Everything works fine until i try to persist the data's then symfony just melt in pure agony and do a nonsense thing persist stuff. It mix the one field of my first entity with the three other fields of my second entity.
Basicaly my builder :
if($options['diplomas_only']){
$builder->add('acquired_diploma', new DiplomaType(), array('viewer_role' => $options['viewer_role']))
->add('prepared_diploma', new DiplomaType(), array('viewer_role' => $options['viewer_role'], 'prepared' => true))
->add('save', 'submit');
}
my diploma type :
$builder
->add('level', new DiplomaLevelType(), array('disabled' => false))
->add('category', new DiplomaCategoryType(), array('disabled' => false))
->add('title', new DiplomaTitleType(), array('disabled' => false))
->add('blueorwhite', 'text', array('disabled' => true));
My Controller :
/**
* @Route("/{_locale}/cursus/{contractId}", requirements={"_locale" = "fr|en"}, defaults={"_locale" = "fr"}, name="poljeunefront_cursus")
*/
public function cursusAction ($locale = 'fr', Request $request, $contractId) {
//get contract
$em = $this->getDoctrine()->getManager();
$contract = $em->getRepository('PolJeuneAdminBundle:Generic_Contract')->findOneById($contractId);
$viewer = $this->get('security.context')->getToken()->getUser();
$formGenericContract = $this->get('form.factory')
->createBuilder(new Generic_ContractType(), $contract, array('diplomas_only' => true, 'viewer_role' => $viewer->getCurrentRole()))
->getForm()
->handleRequest($request);
if ($viewer->isBeneficiary()) {
if ($formGenericContract->get('save')->isClicked()) {
if ($formGenericContract->isValid()) {
var_dump($formGenericContract->get('acquired_diploma'));
exit();
$em = $this->getDoctrine()->getManager();
$em->persist($contract);
$em->flush();
$savingStatus = true;
}
else {
$savingStatus = false;
}
}
}
return $this->render('PolJeuneFrontBundle:Contract:cursus.html.twig', array(
'formGenericContract' => $formGenericContract->createView(),
));
}
and finaly my twig :
{% set formAttributes = readonly is defined and readonly == true ? {} : { 'data-parsley-validate': '', 'data-parsley-excluded': '[disabled]' } %}
{% form_theme formGenericContract "PolJeuneFrontBundle:Form:form_theme.html.twig" %}
{{ form_start(formGenericContract, {
'attr': formAttributes
}) }}
<h3>{{ 'cursus.title'|trans({}, 'contract') }}</h3>
<h4>{{ 'prepared.title'|trans({}, 'contract') }}</h4>
<div class="row">
<div class="col-sm-3">
{{ form_row(formGenericContract.prepared_diploma.level.level) }}
</div>
<div class="col-sm-3">
{{ form_row(formGenericContract.prepared_diploma.category.category) }}
</div>
<div class="col-sm-3">
{{ form_row(formGenericContract.prepared_diploma.title.title) }}
</div>
<div class="col-sm-3">
{{ form_row(formGenericContract.prepared_diploma.blueorwhite) }}
</div>
</div>
<hr>
<h4>{{ 'acquired.title'|trans({}, 'contract') }}</h4>
<div class="row">
<div class="col-sm-3">
{{ form_row(formGenericContract.acquired_diploma.level.level) }}
</div>
<div class="col-sm-3">
{{ form_row(formGenericContract.acquired_diploma.category.category) }}
</div>
<div class="col-sm-3">
{{ form_row(formGenericContract.acquired_diploma.title.title) }}
</div>
<div class="col-sm-3">
{{ form_row(formGenericContract.acquired_diploma.blueorwhite) }}
</div>
</div>
{{ form_row(formGenericContract.save) }}
{{ form_end(formGenericContract) }}
<hr>
Thank's for reading