Details Page and Updating ToDo Items

Welcome back! In our previous lessons, we set up the foundation of our MVC ToDo App in Symfony. We've learned how to manage our todos with a TodoService and added new items to our list. Now, it's time to enhance our application by allowing users to view and update individual ToDo items.

Being able to view and edit ToDo items is crucial for any todo list application, as it enables users to modify existing tasks as their needs change. By the end of this lesson, you will be able to view details of a ToDo item and update it through a dedicated details page.

Implementing the FindOne Method in the Service
Implementing the Update Logic in the Service
Setting Up the Route for Details Page
Setting Up the Route for Updating ToDo Items
Building the Details Page View

Finally, let's create the Twig template that will render the details of a specific ToDo item. Here is the detail.html.twig template:

<!DOCTYPE html>
<html>
<head>
  <title>ToDo Details</title>
</head>
<body>
  <h1>Todo Details</h1>
  
  <!-- Form for updating the ToDo item -->
  <form action="{{ path('todo_update', {'id': todo.id}) }}" method="post">
    <div>
      <label for="title">Title:</label>
      <!-- Input for updating the title -->
      <input type="text" id="title" name="title" value="{{ todo.getTitle() }}" required>
    </div>
    <div>
      <label for="description">Description:</label>
      <!-- Input for updating the description -->
      <input type="text" id="description" name="description" value="{{ todo.getDescription() }}">
    </div>
    <div>
      <!-- Button to submit the form and update the ToDo item -->
      <button type="submit">Update To Do</button>
    </div>
  </form>
  
  <!-- Link to go back to the list view -->
  <p><a href="{{ path('todo_list') }}">Go back to the list</a></p>
</body>
</html>

In this template, we have a form that allows the user to edit the title and description of the ToDo item. The form's action attribute points to the todo_update route while passing the ToDo item's ID as a parameter. The fields are pre-filled with the current title and description values of the ToDo item. When the form is submitted via the "Update To Do" button, the data will be sent using the POST method to the update action in the controller.

Updating the List Page to Link to Details Page

To make it easy for users to navigate to the details page of any ToDo item, we need to update our list view. We will modify the list.html.twig template so that each ToDo item redirects to the details page when clicked.

Here is the updated list.html.twig template:

<!DOCTYPE html>
<html>
<head>
  <title>ToDo List</title>
</head>
<body>
  <h1>ToDo List</h1>
  <ul>
    {% for todo in todos %}
      <li>
        <!-- Link to the details page for each ToDo item -->
        <a href="{{ path('todo_detail', {'id': todo.id}) }}">
          {{ todo.getTitle() }} - {{ todo.getDescription() }}
        </a>
      </li> 
    {% else %}
      <li>No todos found</li>
    {% endfor %}
  </ul>
  
  <form action="{{ path('todo_create') }}" method="post">
    <input type="text" name="title" placeholder="Title" required>
    <input type="text" name="description" placeholder="Description">
    <button type="submit">Add To Do</button>
  </form>
</body>
</html>

In this updated template, we loop through all ToDo items and create a list item (<li>) for each one. Inside each list item, there's a link (<a>) that points to the todo_detail route, passing the respective ToDo item's ID as a parameter. This allows users to click on any ToDo item in the list and be redirected to the details page of that specific item. Additionally, the form at the bottom allows users to add new ToDo items.

Example Walkthrough

Let's go through a complete example, from selecting an item from the list page to updating a ToDo item.

  1. Navigate to details page: Click on a ToDo item link on the list page to access its details.
  2. Edit form fields: Modify the title and description.
  3. Submit form: Click "Update To Do" to send data via POST.
  4. Controller processes update: update method in TodoController processes form data.
  5. Service updates item: TodoService updates the ToDo item in the session.
  6. View updated list: Redirected list view shows the updated item.
Summary and Next Steps

In this lesson, we enhanced our ToDo app by adding the functionality to view and update ToDo items. We created the details page by

  • Setting up the route and controller action to handle displaying the details of a ToDo item
  • Implementing the update logic in the service layer
  • Building the details page view using Twig.

Finally, we walked through the entire process from accessing the details page to updating a ToDo item. Now it's your turn to practice what you've learned. Head over to the practice exercises and try updating ToDo items in your list. Remember, the more you practice, the better you'll become. Great job!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal