0

I am getting NotFoundHttpException error when I try to create new entity with form.

This is a code for creating form and entity - CategoryController:

 /**
 * Displays a form to create a new Category entity.
 *
 * @Route("/new", name="category_new")
 * @Method({"GET"})
 */
public function newAction(Request $request)
{
    $entity = new Category();
    $form   = $this->createCreateForm($entity);


    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}

/**
 * Creates a new Category entity.
 *
 * @Route("/", name="category_create")
 * @Method("POST")
 * @Template("AdminBundle:CategoryPanel:new.html.twig")
 */
public function createAction(Request $request)
{
    $entity = new Category();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $em->persist($entity);
        $em->flush();

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

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}

/**
 * Creates a form to create a Category entity.
 *
 * @param Category $entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createCreateForm(Category $entity, ServiceCategory $parentCategory = null)
{
    $form = $this->createForm(CategoryType::class, $entity, array(
        'action' => $this->generateUrl('category_create'),
        'method' => 'POST',
        'parentCategory' => $parentCategory
    ));

    $form->add('submit', SubmitType::class, array(
        'label' => 'Create',
        'attr' => array(
            'class' => "btn btn-primary"
        )
    ));

    return $form;
}

CategoryType

      public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name', TextType::class, array('label' => 'Category name'));

    $parentCategory = $options["parentCategory"];

    if($parentCategory != null){
        $builder->add('parent', 'entity', array(
            'class' => "CoreBundle:ServiceCategory",
            'choices' => array($parentCategory)
        ));
    }else{
        $builder->add('parent', 'entity', array(
            'class' => "CoreBundle:ServiceCategory",
            'query_builder' => function(ServiceCategoryRepository $cp){
                $qb = $cp->createQueryBuilder('c');
                return $qb;
            },
        ));
    }

}

Why this code is looking for entity when I am only attempting to create it?

UPDATE

new.html.twig

{% extends 'AdminBundle:AdminPanel:base.html.twig' %}

{% block body -%}
<h1>Category creation</h1>

{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.parent) }}

<ul class="record_actions">
    <li style="display: inline-block">
        {{ form_widget(form.submit) }}
    </li>
    <li style="display: inline-block">
        <a href="{{ path('category_panel_index') }}">
            <button type="button" class="btn btn-primary">
                Back to the list
            </button>
        </a>
    </li>
</ul>
{{ form_end(form) }}

{% endblock %}

2
  • There is no template configured to your newAction. Commented Jul 25, 2016 at 16:45
  • @Alsatian I configured my template, just didn't include it in question. Because error has nothing to do with template. Commented Jul 26, 2016 at 7:28

1 Answer 1

1

This might be a conflict between multiple routes as it happened in my case.

You might have some other route may be in some other controller having similar path (with dynamic varaibles) making <>/new pointing somewhere else.

Please do a var_dump in your newAction Controller to check if the execution is coming right there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.