Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created June 11, 2010 20:23
Show Gist options
  • Save jmikola/434986 to your computer and use it in GitHub Desktop.
Save jmikola/434986 to your computer and use it in GitHub Desktop.
Using grouped constraints with Symfony 2 Forms
<?php
/* Since the group name is specified, "Default" will be assigned automatically and
* the implicity group name "Registration" (based on containing class' name) will
* also not be assigned.
*/
class Registration
{
public $field;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata
->addPropertyConstraint('field', new MinLength(array(
'limit' => 4,
'groups' => 'Test'
));
}
}
$registration = new Registration();
$form = new RegistrationForm('registration', $registration);
// See: http://github.com/bschussek/symfony/commit/faed30dfec570ca474af7498700df9816cf985e1
$form->setValidationGroups('Test');
if ($this->getRequest()->request->has('registration')) {
$form->bind($this->getRequest()->request->get('registration'));
if ($form->isValid()) {
// Do something
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment