Adding ToDo Items to the List

Welcome back! In the previous lesson, we set up the foundation of our MVC ToDo App in Symfony. We created a Todo entity, set up a TodoService to manage our todos, and defined a TodoController to handle requests and render a list of todos using Twig.

Today, we're going to add a critical feature: the ability to add new ToDo items to our list. By the end of this lesson, you will be able to create new tasks and see them displayed in the list.

Defining a Create Method in TodoService
Configuring the Create Route in the Controller
Including a Form to the Template

Now, let's update our Twig template to include a form that allows users to add new ToDo items. 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>{{ todo.getTitle() }} - {{ todo.getDescription() }}</li>
    {% else %}
      <li>No todos found</li>
    {% endfor %}
  </ul>
  <!-- Form to add new ToDo items -->
  <form action="{{ path('todo_create') }}" method="post">
    <input type="text" name="title" placeholder="Title" required>
    <input type="text" name="description" placeholder="Description">
    <!-- Submit button to add the new ToDo item -->
    <button type="submit">Add To Do</button>
  </form>
</body>
</html>

In this template, the form's action points to the todo_create route, which we defined in the controller. The form uses the POST method to send data to the server and contains two input fields: one for the title (required) and one for the description (optional). A submit button is included to add a new ToDo item when clicked.

Example Walkthrough

Let's walk through an example of adding a new ToDo item.

  1. Fill the Form: Enter a title and an optional description in the form fields.

  2. Submit the Form: Click the Add To Do button.

  3. Form Submission: The form data is sent to the /todos/create endpoint as a POST request.

  4. Controller Handling: The TodoController extracts the form data and calls the create method of the TodoService.

  5. Service Handling: The TodoService creates a new Todo item and stores it in the session.

  6. Redirection: The controller redirects back to the list view, where the new ToDo item is now visible.

Summary and Next Steps

In this lesson, we expanded our ToDo app by adding the functionality to create new ToDo items. Here is a summary of what we've covered:

  1. Updated the TodoService: We added a create method to handle the creation of new ToDo items and save them in the session.

  2. Configured the Controller: We modified the TodoController to process form submissions and call the service method to create new ToDo items.

  3. Modified the Template: We updated the list.html.twig template to include a form for adding new ToDo items.

Now it's your turn to practice what you've learned. Head over to the practice exercises and try adding new ToDo items to your list. Remember, practice makes perfect!

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