Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8261657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:29:05+00:00 2026-06-08T03:29:05+00:00

I’m currently working with Symfony2 and I’m testing my project with PHPUnit. I want

  • 0

I’m currently working with Symfony2 and I’m testing my project with PHPUnit.
I want to test an exception when a form is submitted with the wrong parameters or the URL isn’t complete.

I went trough the documentation of Symfony2 and PHPUnit but didn’t find any class/method to do so.

How can I change the value of a form’s action? I want to use PHPUnit so the report created is up to date and I can see the coverage of my code.

EDIT:

To clarify my question, some new content.
How do I test the line starting with ‘>’ in my controller? (throw $this->createNotFoundException('Unable to find ParkingZone entity.');)

When the user modifies the action link, in the controller the process will go trough the exception (or error message, if this action is chosen). How can I test this case?

Controller

/**
 * Edits an existing ParkingZone entity.
 *
 * @Route("/{id}/update", name="parkingzone_update")
 * @Method("post")
 * @Template("RatpGarageL1Bundle:ParkingZone:edit.html.twig")
 */
public function updateAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('RatpGarageL1Bundle:ParkingZone')->find($id);

    if (!$entity) {
>        throw $this->createNotFoundException('Unable to find ParkingZone entity.');
    }

    $editForm   = $this->createForm(new ParkingZoneType(), $entity);
    $deleteForm = $this->createDeleteForm($id);

    $request = $this->getRequest();

    $editForm->bindRequest($request);

    if ($editForm->isValid()) {
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('parkingzone_edit', array('id' => $id)));
    }

    return array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );
}

View:

<form action="{{ path('parkingzone_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(form) }}>
    <div class="control-group">
        {{ form_label(form.name, 'Name', { 'attr': {'class': 'control-label'} } ) }}
        <div class="controls error">
            {{ form_widget(form.name, { 'attr': {'class': ''} } ) }}
            <span class="help-inline">{{ form_errors(form.name) }}</span>
        </div>
    </div>
    <div class="control-group">
        {{ form_label(form.orderSequence, 'Rang', { 'attr': {'class': 'control-label'} } ) }}
        <div class="controls error">
            {{ form_widget(form.orderSequence, { 'attr': {'class': ''} } ) }}
            <span class="help-inline">{{ form_errors(form.orderSequence) }}</span>
        </div>
    </div>
    <div class="control-group">
        {{ form_label(form.image, 'Image', { 'attr': {'class': 'control-label'} } ) }}
        <div class="controls error">
            {{ form_widget(form.image, { 'attr': {'class': ''} } ) }}
            <span class="help-inline">{{ form_errors(form.image) }}</span>
        </div>
    </div>
    {{ form_rest(form) }}
    <div class="form-actions">
        <button type="submit" class="btn btn-primary">Enregistrer</button>
        <a href="{{ path('parkingzone') }}" class="btn">Annuler</a>
    </div>
</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:29:08+00:00Added an answer on June 8, 2026 at 3:29 am

    Symfony itself does not have any objects through which it is possible to manipulate the form’s action as it is set in the html (twig files). However, twig provides the capability to dynamically change the form’s action in the twig file.

    The basic approach is for the controller to pass a parameter into the twig file via the render call. Then the twig file can use this parameter to set the form action dynamically. If the controller uses a session variable to determine the value of this parameter then by setting the value of this session variable in the test program it is possible to set the form action specifically for the test.

    For example in the controller:

    public function indexAction()
    {
        $session = $this->get('session');
        $formAction = $session->get('formAction');
        if (empty($formAction)) $formAction = '/my/normal/route';
    
        ...
    
        return $this->render('MyBundle:XXX:index.html.twig', array(
            'form' =>  $form->createView(), 'formAction' => $formAction)
        );
    }
    

    And then, in the twig file:

    <form id="myForm" name="myForm" action="{{ formAction }}" method="post">
    ...
    </form>
    

    And then, in the test program:

    $client = static::createClient();
    $session = $client->getContainer()->get('session');
    $session->set('formAction', '/test/route');
    $session->save();
    
    // do the test
    

    This isn’t the only way, there are various possibilities. For example, the session variable could be $testMode and if this variable is set the form passes $testMode = true into the render call. Then the twig file could set the form action to one of two values depending on the value of the testMode variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
i want to parse a xhtml file and display in UITableView. what is the
I am currently running into a problem where an element is coming back from
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.