Pregunta

I'm trying to solve this little problem. I have a form that allows to modify datas to a db, but when I click on the submit button(even if it changes the values), it shows an error: Some mandatory parameters are missing ("name") to generate a URL for route "update_page". 500 Internal Server Error - MissingMandatoryParametersException

Can anybody help me?

This is my controller:

class updateElementController extends Controller
{       
public function updateAction($name)
{
    $request = $this->get('request');
    $ingrediente = $this->getDoctrine()
    ->getRepository('MyCrudBundle:Elements')
    ->findOneByName($name);


    $em = $this->getDoctrine()->getManager();

    $elementsmod = new Elements();

    $formupdate = $this->createFormBuilder($elementsmod)
        ->add('name', 'text')
        ->add('quantity', 'text')
        ->add('Change element', 'submit')
        ->getForm();

    $formupdate->handleRequest($request);

    if ($formupdate->isValid()) {
    $newval = $formupdate->getData();

    // change "entity" / object values we want to edit
    $namevalue= $newval->getName();
    $quantityvalue= $newval->getQuantity();
    $element->setName($namevalue);
    $element->setQuantity($quantityvalue);

    // call to flush that entity manager from which we create $item
    $em->flush();

    return new RedirectResponse($this->generateUrl('update_page'));
    }

    return $this->render('MyCrudBundle:Default:index.html.twig', array(
    'form' => $formupdate->createView(),
    )); 
}
}

And my route:

update_page:
pattern:  /updatePage/{name}
defaults: { _controller: MyCrudBundle:updateElement:update }

(if you need other parts of code, just tell me, I'm a beginner and most certainly it's horribly written)

¿Fue útil?

Solución

I suggest two options:

  1. If the name is not mandatory in your routing you can change your routing as below

    update_page: pattern: /updatePage/{name} defaults: { _controller: MyCrudBundle:updateElement:update, name: null }

  2. If the name is mandatory for the generated url you need to pass the name as the pattern param

    if ($formupdate->isValid()) { $newval = $formupdate->getData(); $namevalue= $newval->getName(); $quantityvalue= $newval->getQuantity(); $element->setName($namevalue); $element->setQuantity($quantityvalue); $em->flush(); return new RedirectResponse($this->generateUrl('update_page', array('name' => $name))); }

base on your query selection you have in your controller the $name is mandatory and I suggest to try the second one

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top