Reputation: 161
I cant find data in my database when I submit the form and i got no error , I need your help
I can't insert data into database symfony2 doctrine if you noticed an error please mention it // this is my contact action
public function contactUsAction() { // contact action
$contact = new Contact();
$form = $this->createForm(new ContactType(), $contact);
$request = $this->getRequest();
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($contact);
$em->flush();
$this->get('session')->getFlashBag()->add('Notice', 'Votre message a été bien envoyé');
}
}
return $this->render('carRentalMainBundle:Main:contactUs.html.twig', array(
'form' => $form->createView(),
));
}
// And this is my view
this is my view
{% extends 'carRentalMainBundle::layout.html.twig' %}
{% block body %}
<div class="col-md-6">
<h3>Contactez-nous</h3>
{% for flashMessage in app.session.flashbag.get('Notice') %}
<ul><li class="alert-success">
{{flashMessage}}
</li></ul>
{% endfor%}
<br/>
<form method="POST" action="{{path('car_rental_main_contact')}}" {{form_enctype(form)}} >
<div class='form-group'>
{{form_errors(form.name)}}
{{form_label(form.name,' Nom :')}}
{{form_widget(form.name)}}
</div>
<div class='form-group'>
{{form_errors(form.email)}}
{{form_label(form.email,' Email :')}}
{{form_widget(form.email)}}
</div>
<div class='form-group'>
{{form_errors(form.subject)}}
{{form_label(form.subject,' Sujet :')}}
</div>
{{form_widget(form.subject)}}
<div class='form-group'>
{{form_errors(form.content)}}
{{form_label(form.content,'Message :')}}
{{form_widget(form.content)}}
</div>
{{form_rest(form)}}
<input type="submit" class="btn-primary btn" value="Envoyer" >
</form>
</div>
{% endblock %}
Thank you for your help
Upvotes: 0
Views: 855
Reputation: 201
I would like to give a little advice if version of Symfony you use >=2.3 you can use $form->handleRequest($request)
to reduce code. More information about it you can get from here ...
About you problem, you can set{{form_errors(form)}}
in you template, as described above. Or get error report in you controller by $form->getErrorsAsString()
for example.
Code Example:
public function contactUsAction(Request $request) { // contact action
$contact = new Contact();
$form = $this->createForm(new ContactType(), $contact);
$form->handleRequest($request);
if ($form->isValid())
{
// if need to catch database flush exceptions
try
{
$em = $this->getDoctrine()->getManager();
$em->persist($contact);
$em->flush();
$this->get('session')->getFlashBag()->add('Notice', 'Votre message a été bien envoyé');
} catch (\Exception $e)
{
$this->get('session')->getFlashBag()->add('Notice', $e->getMessage());
}
}
return $this->render('carRentalMainBundle:Main:contactUs.html.twig', array(
'form' => $form->createView(),
// for debug only
'form_errors_list' => $form->getErrorsAsString(),
));
}
and in your template you can set (only for debug proccess):
<pre>{{dump(form_errors_list)}}</pre>
Upvotes: 2
Reputation: 64
Is there flashMessage after submit? If no, then add {{form_errors(form)}} to see form errors.
Upvotes: 0