Skip to content

Instantly share code, notes, and snippets.

@johnwards
Created May 16, 2011 13:41
Show Gist options
  • Save johnwards/974450 to your computer and use it in GitHub Desktop.
Save johnwards/974450 to your computer and use it in GitHub Desktop.
<?php
namespace Foo\AccountBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Validator\Constraints\Callback;
class CreatePasswordFormType extends AbstractType
{
public $notmatch_message = "The passwords don't match, please try again";
public $length_message = "Your password should be %s characters in length";
public $password_length = 8;
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('first', 'password', array("error_bubbling"=>true))
->add('second', 'password');
$notmatch_message = $this->notmatch_message;
$length_message = $this->length_message;
$length = $this->password_length;
$builder->setAttribute('validation_constraint', new Callback(array('methods' => array(
'validate' => function ($data, $context) use ($notmatch_message, $length_message, $length) {
if ($data["first"] !== $data["second"]) {
$context->setPropertyPath('children[first]');
$context->addViolation($notmatch_message, array(), null);
} elseif (strlen($data["first"]) < $length) {
$context->setPropertyPath('children[first]');
$context->addViolation(sprintf($length_message, $length), array(), null);
}
}
))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment