I want to know how I can make sure that I will not receive a null value from a form within Symfony, server side. The code I am using now:
public function insertAction(Request $request)
{
$movie = new Movie();
if($request->get('title') != null)
{
$movie->setTitle($request->get('title'));
}
if(str_replace('.', '', $request->get('price')) != null)
{
$movie->setPrice(str_replace('.', '', $request->get('price')));
}
if($request->get('description') != null)
{
$movie->setDescription($request->get('description'));
}
$em = $this->getDoctrine()->getManager();
$em->persist($movie);
$em->flush();
return $this->redirectToRoute('movies');
}
But whenever I send an empty form I still get to see the following error:
An exception occurred while executing 'INSERT INTO movie (title, price, description) VALUES (?, ?, ?)' with params [null, null, null]:SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null
. What can I do to improve my code? Thanks in advance.
Edit: This is my solution: http://pastebin.com/4Z088SNz