0

In a page, i have some element which users can click to edit. I want to add a function to edit the date with a simple drag ot the event in a calendar grid. The user could quickly edit the date by dragging the event down, or click on the event to edit more things.

For now, I want to persist my entity with an ajax call when the user click on an event. I can retrieve my data (event' id and the start_date)I tried to put them in a simple .txt file and it work.

My ajax :

var start_date = event.start_date;
var id = id;
var edit = Routing.generate('admin_event_edit', {id : id});

$.ajax({
    url: Routing.generate('admin_event_edit', {id : id}),
    type: "POST",
    data: { id : id, start_date : start_date},
    success: function(data) {
        alert('ok');
    },
    error: function(){
        alert('error');
    }
});

i have a form at the route admin_event_edit And in my controller :

public function editAction(Request $request, $id)
{
     $em = $this->getDoctrine()->getManager();
     $entity = $em->getRepository('AppBundle:Event')->find($id);
     $starDate = $request->request->get('start_date');

    $editForm = $this->createForm(new EventType(), $entity);

    $editForm->handleRequest($request);

    if ($editForm->isValid())
    {
        if($request->isXmlHttpRequest())
        {

            $entity->setStartDate($starDate);
            $entity->setNotice('azeazedfdf');

            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();
        }

    }

        return $this->redirect($this->generateUrl('admin_index'));

}

I have the ajax alert ok and i can see the ajax call has been sent in the symfony profiler, but my entity isn't updated. What's wrong with my code ?

Thanks

2
  • what is the return of $editForm->isValid()? Commented Dec 29, 2015 at 13:27
  • My bad, in my question i put it in the wrong place Commented Dec 29, 2015 at 13:29

2 Answers 2

2

The $form isn't valid.

The Symfony Form needs the CSRF Token to be valid, it's an hidden field within the generated form.

Since you're submitting this form with AJAX instead of the normal way, the isValid() check will always fail, because you don't send all necessary data in your AJAX request.

Example:

<input id="form__token" class="form-control" type="hidden" value="Poo4GKPb2pcchnRXAJd74ZxLqTU2xMrgHKi8QHN4N8Y" name="form[_token]">

You need to send this value with your AJAX call, if no more information is missing your code should work as far as I can tell.

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

1 Comment

I don't understand why i need the CSRF token here : My events list (and my ajax) is on a page named index.html.twig (there is no form in index.html ). My ajax is sending data from index.html to a second page named edit.html where there is a form. I can't get to token of page 2 from page 1 no ? But you pointed out something interesting : my form (from page 2 edit.html) isn't sent (or valid). i'll dig from here then come back
0

I made it work. In fact i didn't needed to go through my form :

public function editAction(Request $request, $id)
{
     $em = $this->getDoctrine()->getManager();
     $entity = $em->getRepository('AppBundle:Event')->find($id);
     $startDate = $request->request->get('start_date');
     $endDate = $request->request->get('end_date');

    if ($request->isMethod('POST'))
     {

        if($request->isXmlHttpRequest())
        {
            $entity->setStartDate(new \DateTime($startDate));
            $entity->setEndDate(new \DateTime($endDate));
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

        }

    }

    return //your stuff, me i don't need to be redirected
}

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.