I\'ve got a problem with a Symfony2 generated CRUD form. (With MongoDB Documents, but I do not think that this is related)
In my controller\'s createAction() method,
Update for Symfony 2.6
So depending on you Symfony2 version:
symfony2.3
die($form->getErrorsAsString());
As of symfony2.5, the getErrorsAsString()
function is deprecated (will be removed in Symfony3) and you should use the following method:
die((string) $form->getErrors()); // Main errors
die((string) $form->getErrors(true)); // Main and child errors
As of symfony2.6, you can also use the dump
(dev environment) function if you have activated the DebugBundle
:
dump((string) $form->getErrors()); // Main errors
dump((string) $form->getErrors(true)); // Main and child errors
I came across this error and found that I was forgetting to "handle" the request. Make sure you have that around...
public function editAction(Request $request)
{
$form = $this->createForm(new CustomType(),$dataObject);
/** This next line is the one I'm talking about... */
$form->handleRequest($request);
if ($request->getMethod() == "POST") {
if ($form->isValid()) {
...
From Symfony 3 onwards as per documentation you should do use the new implementation:
$errors = (string) $form->getErrors(true, false);
This will return all errors as one string.
It appears as you have a validation problem. The form is not validating on submitting. I am going to assume you are using Annotations for your validation. Make sure you have this at the top of the entity.
use Symfony\Component\Validator\Constraints as Assert;
and also this above each property
/**
* @Assert\NotBlank()
*/
The NotBlank()
can be changed to any constraint to fit your needs.
More information on validation can be found at: http://symfony.com/doc/current/book/validation.html
More information on Assert constraints can be found at: http://symfony.com/doc/current/book/validation.html#constraints
For Symfony (>= 3.2 - 4), you can use :
foreach($form->getErrors(true, false) as $er) {
print_r($er->__toString());
}
to see the errors obviously.