Skip to content

Instantly share code, notes, and snippets.

@nqbao
Created July 27, 2011 07:32
Show Gist options
  • Save nqbao/1108862 to your computer and use it in GitHub Desktop.
Save nqbao/1108862 to your computer and use it in GitHub Desktop.
Extra field for entity binding and custom validation in Synfomy 2
<?php
namespace HiveNet\BaseBundle\Form;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormValidatorInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class UserType extends AbstractType implements FormValidatorInterface
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('email')
->add('password1', 'password', array('label' => 'Password', 'property_path' => ''))
->add('password2', 'password', array('label' => 'Retype Password', 'property_path' => ''))
->add('firstName', 'text', array('label' => 'First name'))
->add('lastName', 'text', array('label' => 'Last name'))
->add('language')
->addValidator($this)
;
}
public function validate(FormInterface $form)
{
$pass1 = $form->get('password1')->getData();
$pass2 = $form->get('password2')->getData();
if ($pass1 != $pass2)
{
// TODO: add translation
$form->get('password2')->addError(new FormError("Retype password does not match"));
}
}
public function getName()
{
return 'hivenet_basebundle_usertype';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment