Built in form validation using $form->isValid() fails by always returning true. I have the static loadValidatorMetadata() method in a plain Php model class. A workaround like this works, though:
$violations = $app['validator']->validate($person);
The full route is this (I manually inject the violations back into the proper form fields; what a hack!):
$app->post('/register/save', function() use ($app) {
$person = new Entities\Person();
$action = $app['url_generator']->generate('save_registration');
$form = $app['form.factory']->createBuilder(new Entities\Person\PersonForm(), $person, array('csrf_protection' => false))->getForm();
$request = $app['request'];
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
$form_elements = $form->getChildren();
$violations = $app['validator']->validate($person);
$count_of_violations = $violations->count();
if ( empty($count_of_violations) ) { //$form->isValid() doesn't work yet...
$data = $form->getData();
$app['doctrine.orm.em']->persist($person);
$app['doctrine.orm.em']->flush();
/* implement post-redirect-get pattern */
return $app->redirect('/register/complete');
} else {
// render the template with the errors, because it doesn't work on its own right now!
foreach ( $form_elements as $element ) {
foreach ( $violations as $error ) {
$error_name = $error->getPropertyPath();
$form_element_name = $element->getName();
if ( $error_name === $form_element_name ) {
$element->addError(new FormError($violations[0]->getMessage()));
}
}
}
return $app['twig']->render('registration/form.phtml', array('form' => $form->createView(), 'action' => $action));
}
}
return $app['twig']->render('registration/form.phtml', array('form' => $form->createView(), 'action' => $action));
}, "GET|POST")->bind('save_registration');
namespace Entities;
//use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Entity
* @Table(name="people")
*/
class Person {
/**
* @Id
* @Column(name="id", type="integer", nullable=false))
* @GeneratedValue
*/
private $id;
/**
* @Column(name="first_name", type="string", length=64)
* @Assert\NotBlank()
* @Assert\MinLength(2)
* @Assert\MaxLength(64)
*/
private $firstname;
/**
* @Column(name="last_name", type="string", length=64)
* @Assert\NotBlank()
* @Assert\MinLength(2)
* @Assert\MaxLength(64)
*/
private $lastname;
static public function loadValidatorMetadata(\Symfony\Component\Validator\Mapping\ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('firstname', new Assert\NotNull());
$metadata->addPropertyConstraint('firstname', new Assert\NotBlank(array(
'message' => 'You must enter a first name',
)));
$metadata->addPropertyConstraint('firstname', new Assert\MinLength(array('limit' => 2, 'message' => 'A first name should have more than {{ limit }} characters in it.')));
$metadata->addPropertyConstraint('firstname', new Assert\MaxLength(64));
$metadata->addPropertyConstraint('lastname', new Assert\NotNull());
$metadata->addPropertyConstraint('lastname', new Assert\NotBlank(array(
'message' => 'You must enter a last name',
)));
$metadata->addPropertyConstraint('lastname', new Assert\MinLength(array('limit' => 2, 'message' => 'A first name should have more than {{ limit }} characters in it.')));
$metadata->addPropertyConstraint('lastname', new Assert\MaxLength(64));
}