I am trying to save HTML content with the Symfony2 form system but I am facing issues with escaping.
My addPost action looks like this
$em = $this->getDoctrine()->getEntityManager();
$post = new Post();
$postForm = $this->createForm(new PostFormType(), $post);
if ($request->getMethod() == 'POST') {
$postForm->bindRequest($request);
if($postForm->isValid()){
$em->persist($post);
$em->flush();
}
}
The problem is that the post form has a content textarea that allows the user to enter html. When the form is submitted with html like <a href="#">test</a> the content gets saved to the database as <a href=\"#\">test</a>. And then on each subsequent save the backslashes escape themselves again and again…
What is the proper way to store HTML with the Symfony2 form component?
You should remove the backslashes from the string before sending it to the user to edit. You should be able to get the content from the $post object and then strip its slashes and set it back to the post content like so.
Now when the form renders it will not have the escape characters.
However, I have found that if you return a response to the same page you will still see the backslashes. To avoid this you can return a RedirectResponse like this