Can I invalidate a Symfony2 form? - symfony

I have an issue in that a Symfony2 form can pass validation but still generate a Doctrine2 exception caused by a unique constraint when the form is submitted. I can catch this PDOException, but what I want to do is invalidate the form and set a form error indicating that a particular attribute of the entity is a duplicate. The code I have is:
$entity = new Tag();
$request = $this->getRequest();
$form = $this->createForm(new \Acme\AdminBundle\Form\Tag(), $entity);
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
try {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('tag_edit', array('id' => $entity->getTagId())));
} catch( ORM\PDOException $e) {
if ($e->getCode() === '23000') {
// What do I do here??
}
}
}
return array(
'entity' => $entity,
'form' => $form->createView()
);

I think you are looking for the UniqueEntity annotation.
If you use it, you won't need the try/catch block, because a check will be performed before an insert is even attempted.

Related

can't update entity Symfony2

Trying to update my entity, unfortunately didnt use Doctrines generate CRUD feature (have to change stuff I didnt write).
I am finally getting data into my form, but it just won't save the changes (also, it doesn't create a new entity as one might suspect).
When I click 'save', I always return to the page where I have my form to edit the entity.
Checked if method is POST, it is.
if ($form->get('save')->isClicked()) {
doesn't seem to do anything, how can that be?
Here's the rest of my action:
/**
* Updates.
*
* #Route("/offerweekchange/{offerid}", name="offerweekchange")
* #Template("")
*/
public function offerweekchangeAction(Request $request, $offerid)
{
$request = $this->get('request');
if ($offerid) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AlexanderBuerkleShopBundle:Promotion')->findOneBy(array('id' => $offerid));
$form = $this->createForm(new OfferWeekChangeType(), $entity);
# \Doctrine\Common\Util\Debug::dump($request->getMethod());
if ($form->get('save')->isClicked()) {
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('offerweeklist'));
}
}
}
return $this->render('AlexanderBuerkleShopBundle:OfferWeekList:offerweekchange.html.twig',
array('form' => $form->createView(), 'offerid' => $offerid, 'entity' => $entity));
}
}
Any help would be greatly appreciated.
First, here is a working sample of your code:
public function offerweekchangeAction(Request $request, $offerid)
{
if ($offerid) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AlexanderBuerkleShopBundle:Promotion')->findOneBy(array('id' => $offerid));
$form = $this->createForm(new OfferWeekChangeType(), $entity);
$form->handleRequest($request);
# \Doctrine\Common\Util\Debug::dump($request->getMethod());
if ($form->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('offerweeklist'));
}
return $this->render('AlexanderBuerkleShopBundle:OfferWeekList:offerweekchange.html.twig',
array('form' => $form->createView(), 'offerid' => $offerid, 'entity' => $entity));
}
}
Secondly, you have several mistakes here, so let's analyse them one by one:
1:
if ($offerid) {
your code does nothing on the else branch.
2:
$request = $this->get('request');
you already have the request parameter injected into the action. This line is redundant.
3:
$form->bind($request);
This is deprecated since 2.3. Use $form->handleRequest($request) instead.
4:
$em->flush();
You are flushing the entity manager, but nothing is persisted, so nothing will happen. you have to persist the entity first with $em->persist($entity)
5:
if ($request->getMethod() == 'POST') {
The method $form->isValid() checks for this also, so checking for post is redundant.
That's it. Hope it helped.
The most likely issue here is with the AbstractType which define your form OfferWeekChangeType).
In order to map the form data on the entity you need to set the 'data_class'.
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
array(
'data_class' => ' Acme\StoreBundle\Entity\Product',
)
);
}
then inside your controller you need to invoke :
$form->handleRequest($request);
This will bind the request to your form.
Only when you invoke:
$form->isValid()
The data from the form (if valid) will be mapped on the entity.
$em->persist($entity);
in this case is superflus because Doctrine UnityOfWork already 'knows' the entity from when you got it from the repository. Just invoke
$em->flush($entity);
As a side note remember that flushing only the needed entity is preferable (when possible) the flush the entire UnityOfWork in order to avoid unexpected behaviours.
Regards.

Call an entity method in a controller

i have three entities: Invoice,Payment and Result
the relationships between entities are:
Result(1,1)-------------(1,n)Invoice(1,n)---------------(1,1)Payment
here's my problem :I would like in my PaymentController when I create a new payement ,I retrieve Invoice entity and in the same PaymentController I create a new Result.
here's my PaymentController code:
use MyApp\AccountBundle\Entity\Result;
class PaymentController extends Controller
public function createAction()
{
$entity = new Payment();
$request = $this->getRequest();
$form = $this->createForm(new PaymentType(), $entity);
$form->bindRequest($request);
$amount=$form->get('amountreceived')->getData();
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$invoice = em->getRepository('MyAppAccountBundle:Invoice')->find($entity->getInvoice()->getId())
if (!$invoice) {
throw $this->createNotFoundException('Unable to find Invoice entity.');
}
$result=new Result();
$result=setDebitAmount($amount);
$result=setCreditAmount(0);
$result=setInvoice($invoice);
$em->persist($result);
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('payment_show', array('id' => $entity->getId())));
}
return $this->render('MyAppAccountBundle:Payment:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView()
));
when a execute PaymentController (in view) i get error:
Fatal error: Call to undefined function MyApp\AccountBundle\Controller\setDebitAmount() in C:\wamp\www\account\src\ MyApp\AccountBundle\Controller\PaymentController.php on line...
thank in advance
Change = to ->
$result=setDebitAmount($amount);
must be
$result->setDebitAmount($amount);

Symfony2 passing parameters to twig

I'm working with Symfony 2 and I want to pass from my controller to my twig template a simple string and then use it on my template to descriminate the user role.
The controller code has something like :
public function modify_user_asAction(Request $request, $username)
{
$stringtopass="admin";
$um = $this->get('fos_user.user_manager');
$user = $um->findUserByUsername($username);
if($user == null){
//error page here..
}
$form = $this->createForm(new UserForm(), $user);
$form->handleRequest($request);
if ($form->isValid()) {
$um->updateUser($user);
return $this->redirect($this->generateUrl('acme_query_success'));
}
return $this->render('AcmeUserBundle:Default:modifyuserform.html.twig', array(
'form' => $form->createView(),
));
}
I want to pass $stringtopass in the generateUrl (if it's possible).
I can't find anything online.
Thanks
You are almost there!
API: generateUrl
Basically, just pass an array as second param to generateUrl.
return $this->redirect($this->generateUrl('acme_query_success', array('stringToPass' => $stringtopass)));
And also, #Brewal has a very valid point there. Be careful not to pass some sensitive data or leave unrestricted access to that controller's action. You could do more harm than good...
UPDATE:
public function acmeQuerySuccessAction(){
// ... action's logic
$stringToPass = $this->getRequest()->query->get('stringToPass');
// .....
return array(
'stringToPass' => $stringToPass,
// all other elements that you would normally return
);
}

symfony doctrine update from form

I'm stuck since this morning with the update of an entity.
Don't know what I'm missing, pretty sure this is a newbie mistake.
I'm just trying to update something via a form.
The controller:
public function editAction($pid, $plid, Request $request)
{
$plan = new Plan();
$form = $this->createForm(new PlanType(), $plan);
$plan = $this->getDoctrine()->getRepository('QArthFrameworkBundle:Plan')->findOneByPlid($plid);
$project = $this->getDoctrine()->getRepository('QArthFrameworkBundle:Project')->findOneByPid($pid);
$form->handleRequest($request);
if ($request->getMethod() == 'POST') {
$em = $this->getDoctrine()->getManager();
$em->flush();
return $this->redirect($this->generateUrl('qarth_framework_plan_edit', array('pid' => $pid, 'plid' => $plid)));
}
return $this->render('QArthFrameworkBundle:Pages:plan_edit.html.twig', array(
'plan' => $plan,
'project' => $project,
'form' => $form->createView(),
));
}
The form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
$builder->add('description', 'textarea');
}
The Entity : http://pastebin.com/bTqKehyQ
With the profiler I can see that my post parameters are well posted
plan {"name":"fsggsfgsf","description":"gsfgsfgsf","_token":"7d089aca0203c60fe1e617488e532ac966101440"}
But I can't see any trace of an update query or something else.
If you have an idea, it will be great!
Many thanks,
Ben
Need to pass the queried plan to the form.
public function editAction($pid, $plid, Request $request)
{
$plan = $this->getDoctrine()->getRepository('QArthFrameworkBundle:Plan')->findOneByPlid($plid);
$project = $this->getDoctrine()->getRepository('QArthFrameworkBundle:Project')->findOneByPid($pid);
// Create a new one if not found
if (!$plan) $plan = new Plan();
// Build your form using queried or new plan
$form = $this->createForm(new PlanType(), $plan);
$form->handleRequest($request);
// Checks for POST as well as validity
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($plan); // To handle new plans, no impact for existting plans
$em->flush();
// Rest is the same

Symfony 2 - separate form logic, show form errors after redirect

I want to separate form validation logic:
public function contactAction()
{
$form = $this->createForm(new ContactType());
$request = $this->get('request');
if ($request->isMethod('POST')) {
$form->submit($request);
if ($form->isValid()) {
$mailer = $this->get('mailer');
// .. setup a message and send it
return $this->redirect($this->generateUrl('_demo'));
}
}
return array('form' => $form->createView());
}
I want to translate into 2 separate actions:
public function contactAction()
{
$form = $this->createForm(new ContactType());
return array('form' => $form->createView());
}
public function contactSendAction()
{
$form = $this->createForm(new ContactType());
$request = $this->get('request');
if ($request->isMethod('POST')) {
$form->submit($request);
if ($form->isValid()) {
$mailer = $this->get('mailer');
// .. setup a message and send it using
return $this->redirect($this->generateUrl('_demo'));
}
}
// errors found - go back
return $this->redirect($this->generateUrl('contact'));
}
The problem is that when errors exist in the form - after form validation and redirect the do NOT showed in the contactAction. (probably they already will be forgotten after redirection - errors context will be lost)
If you check out how the code generated by the CRUD generator handles this you will see that a failed form validation does not return a redirect but instead uses the same view as the GET method. So in your example you would just:
return $this->render("YourBundle:Contact:contact.html.twig", array('form' => $form->createView()))
rather than return the redirect. This means you do not lose the form errors as you do in a redirect. Something else the CRUD generator adds is the Method requirement which means you could specify that the ContactSendAction requires the POST method and thus not need the extra if($request->isMethod('POST')){ statement.
You can also just return an array if you specify the template elsewhere, for example you could use the #Template annotation and then just
return array('form' => $form->createView())
This seems to work for me in Symfony 2.8:
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class MyController extends Controller {
public function templateAction()
{
$form = $this->createForm(new MyFormType(), $myBoundInstance);
if ($session->has('previousRequest')) {
$form = $this->createForm(new MyFormType());
$form->handleRequest($session->get('previousRequest'));
$session->remove('previousRequest');
}
return array(
'form' => $form->createView(),
);
}
public function processingAction(Request $request)
{
$form = $this->createForm(new MyFormType(), $myBoundInstance);
$form->handleRequest($request);
if ($form->isValid()) {
// do some stuff
// ...
return redirectToNextPage();
}
$session->set('previousRequest', $request);
// handle errors
// ...
return redirectToPreviousPage();
}
}
Please note that redirectToNextPage and redirectToPreviousPage, as well as MyFormType, are pseudo code. You would have to replace these bits with your own logic.

Resources