Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

php - Symfony Update data with a post request

I am currently trying to update a Person in a table. The Table looks the following:

Table

I want to be able to update the First-and Lastname and also the nin when I click on the Submit button. My twig file looks like this:

{% block body %}
    <h1>All our Patients</h1>
    <table class="table">
        <thead class="thead-dark">
        <tr>
            <th scope="col">#</th>
            <th scope="col">Firstname</th>
            <th scope="col">Lastname</th>
            <th scope="col">NIN</th>
            <th scope="col">Update</th>
        </tr>
        </thead>
        <tbody>
        {% for person in persons %}
        <tr>
            <form action="{{ path('updatePerson', {"id": person.id}) }}" method="post">
                <td>{{ person.id }}</td>
                <td><input type="text" id="firstName"  value="{{ person.firstName }}"></td>
                <td><input type="text" id="lastName"  value="{{ person.lastName }}"></td>
                <td><input type="text" id="nin" value="{{ person.nin }}"></td>
                <td><input class="btn btn-primary" type="submit" value="Submit"></td>
            </form>
        </tr>
        {% endfor %}
        </tbody>
    </table>
{% endblock %}

And my controller is the following:

/**
     * @Route("/user/persons/{id}", name="updatePerson")
     */
    public function updatePerson(int $id): Response
    {
        $entityManager = $this->getDoctrine()->getManager();
        $person = $entityManager->getRepository(Person::class)->find($id);

        if (!$person) {
            throw $this->createNotFoundException(
                'No person found for id '.$id
            );
        }

        $person->setFirstName('FirstName');
        $person->setLastName('Lastname');
        $person->setNin('00.09.69-420.2');
        $entityManager->flush();

        $persons = $this->getDoctrine()->getRepository(Person::class)->findAll();
        return $this->render('user/person/index.html.twig', [
            'persons' => $persons,
        ]);
    }

Currently when I click update I just fill it what's in the "setFirstName('FirstName');". I don't really know how to pass in the correct data so it updates correctly? how would I obtain the changes I want to make by pressing submit?

question from:https://stackoverflow.com/questions/65907688/symfony-update-data-with-a-post-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The quick fix (not recommended):

You should add the name attribute for text input tags

  <td><input type="text" id="firstName" name="firstName" value="{{ person.firstName }}"></td>
  <td><input type="text" id="lastName" name="lastName" value="{{ person.lastName }}"></td>
  <td><input type="text" id="nin" name="nin" value="{{ person.nin }}"></td>

and modify controller:

/**
 * @Route("/user/persons/{id}", name="updatePerson")
 */
public function updatePerson(Request $request, Person $person): Response
{
    $entityManager = $this->getDoctrine()->getManager();

    $person->setFirstName($request->request('firstName'));
    $person->setLastName($request->request('lastName'));
    $person->setNin($request->request('nin');   

    $entityManager->flush();

    $persons = $this->getDoctrine()->getRepository(Person::class)->findAll();

    return $this->render('user/person/index.html.twig', [
        'persons' => $persons,
    ]);
}

But for the best practice, you should use Symfony Form Collection and add validation rules for fields


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...