Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony my Controller::updateAction() requires that you provide a value for the "$name" argument

Using Symfony 2, I have this error when I try to pass an argument/variable:

Controller "MyBundle\Controller\ManageController::updateAction()" 
requires that you provide a value for the "$name" argument (because there is 
no default value or because there is a non optional argument after this one).

this the entity/model Parcs:

namespace MyBundle\DatabaseBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Parcsimmobilier
 *
 * @ORM\Table(name="parcs")
 * @ORM\Entity
 */
class Parcs
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=150, nullable=false)
     */
    private $name;

First, I have a twig, which can allow users to modify/update a Parc, I'm using JavaScript to allow the select tag in html to display a form in another route/twig when a choice is made, this is the code for choose the parcs users would like to update:

{% block content %}
<div class="form-group">
  <label class="control-label">Which Parcs would you like to update ?</label>
  <form action="{{ path('UpdateParcs_form' , {'name':'parcs.name'}) }}" method="GET">
    <select class="form-control" id="selectpicker">
      <option disabled selected> select your parc </option>
      {% for parcsimmobilier in parc %}
        <option value="parc"><strong>{{ parcs.name}}</strong></option>
      {% endfor %}
    </select>
  </form>
</div>
{% endblock %}

this is the JavaScript code which allow me to redirect users after a choice on the other route which displaying the update form:

$(function() {
    $('#selectpicker').change(function() {
        this.form.submit();
    });
});

And now the twig to who display the form to update the chosen parc:

{% block content %}
    <form action="{{ path('updateParcs_process') }}" method="POST">
      <div>
      {{ form_label(form.name, "change the name of the parc", {'label_attr': {'class': 'control-label'}}) }}
        <div>
          {{ form_widget(form.name, {'attr': {'class': 'form-control'}}) }}
        </div>
      </div>

      <input type="submit" value="Update" class="btn btn-success"/>
    </form>
{% endblock %}

So when I submit on my update Button, I have the error:

Controller "MyBundle\Controller\ManageController::updateAction()" requires that you provide a value for the "$name" argument (because there is no default value or because there is a non optional argument after this one).

This is my controller code with the update action:

public function updateAction($name) {

    $em = $this->getDoctrine()->getManager();
    $parc = $em->getRepository('MySpaceDatabaseBundle:Parcs')->find($name);
    $form = $this->createForm(new ParcsType(), $parc);

    $request = $this->get('request');

    if ($request->isMethod('POST') | ($form->isValid())) {

            $form->bind($request);
            $parc = $form->getData();
            $em->flush();

            return $this->redirect($this->generateUrl('index_parcs'));
        }

    //retourner le formulaire d'ajout si c'est invalide
    else {
            return $this->render('MySpaceManageBundle:Parcs:updateParcs.html.twig', array('form' => $form->createView(), 'parc' => $parc, 'name' => $name));
         }
}

This is the routing file for my routes:

# index for management
index_Parcs:
    path:     /manageparcs
    defaults: { _controller: MySpaceManageBundle:Manage:indexParcs }
    requirements:
    methods: GET

# route for display the form after the select choice
updateParcs_form:
    path:     /manageparcs/update/form/{name}
    defaults: { _controller: MySpaceManageBundle:Manage:update }
    requirements:
    methods: GET

# route for processing form with update
updateParcs_process:
    path:     /manageparcs/update/form/success
    defaults: { _controller: MySpaceManageBundle:Manage:update }
    requirements:
    methods: POST

My questions are these: Why do I have this error? How can I fix it? And how can I update correctly my entity/model with this updateAction(). How can I pass the arguments by a route in Symfony.

like image 446
french_dev Avatar asked Dec 03 '25 14:12

french_dev


2 Answers

updateParcs_form:
    path:     /manageparcs/update/form/{name}
    defaults: { _controller: MySpaceManageBundle:Manage:update }
    requirements:
    methods: GET

# route for processing form with update
updateParcs_process:
    path:     /manageparcs/update/form/success
    defaults: { _controller: MySpaceManageBundle:Manage:update }
    requirements:
    methods: POST

Your second route doesn't specify a name parameter as the first one does.
The cause is that the method is expecting the argument, but based on this configuration none will be provided.

Unless you want to create another action for the POST request, you will need to add the name to the POST URI.

path:     /manageparcs/update/form/{name}/success

And you will need to generate the URI with this parameter.

like image 173
Flosculus Avatar answered Dec 05 '25 04:12

Flosculus


You are doing right things except you have $nom as parameter in your controller but name in your routing configuration. According to other files it will be easy to change only configuration:

Change name in routing.yml to nom:

updateParcs_form:
path:     /manageparcs/update/form/{nom}
defaults: { _controller: MySpaceManageBundle:Manage:update }
requirements:
methods: GET
like image 31
Michael Sivolobov Avatar answered Dec 05 '25 04:12

Michael Sivolobov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!