pcavalet
pcavalet

Reputation: 121

Interaction between two forms in a single controller

I'm trying to work with two forms in a single controller in Symfony2. The page is displaying a list of e-mail addresses with checkboxes to select them.

The first form is a research form and the second is used to remove e-mail addresses. There is also a pagination in my page.

My problem is that I can't remove an e-mail address with a research when the e-mail address is not displayed in the first page.

public function emailsAction(Request $request, $paginationParams)
{
    $searchForm = $this->createForm(new SearchFormType(), null, array());
    $searchForm->handleRequest($request);

    if ($searchForm->isValid()) {
        // FETCH E-MAILS WITH SEARCH
    } else{
        // FETCH E-MAILS WITHOUT SEARCH
    }

    // here I slice the result depending on the pagination params

    $removeForm = $this->createForm(new RemoveFormType(), null, array(
        'emails' => $emails
    ));
    $removeForm->handleRequest($request);

    if ($removeForm->isValid())
    {
        $formData = $removeForm->getData();

        // here I remove the e-mails
    }

    ...
    $params['remove_form'] = $manageSubscribersForm->createView();
    $params['search_form'] = $searchAbonneActuForm->createView();

    return $this->renderView('my_template.html.twig', $params);
}

If I understand it correctly, the problem is that my removeForm is created with the spliced array and without the search applied when handling the removeForm.

When I search for a name, say johnsmith, it displays me on page 1, one result which is [email protected].

If [email protected] is on page 2 without search, the removeForm handling my request is created with a spliced array not containing johnsmith, so the removeForm is not considered valid.

How should I do to create my removeForm, taking account of the search done before when submitting the removeForm? Or maybe I'm doing this wrong ?

I'm not a native english speaker so if anything is not clear, feel free to ask.

Upvotes: 0

Views: 54

Answers (1)

ScreamZ
ScreamZ

Reputation: 602

You can use a hidden field with the current page index, that will help you make your search again.

Or you can use an event listener to modify the form field before the submission for the validation.

Upvotes: 1

Related Questions