LoginSignup
1
1

More than 5 years have passed since last update.

Symfony2.3でcallbackValidatorが使えなくなったのでFormEventで書き換える

Last updated at Posted at 2014-05-09

BEFORE
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('name')
->add('email', 'repeated', array(
'type' => 'email',
'invalid_message' => '同じ値を入力してください',
))
->add('rawPassword', 'password', array(
'always_empty' => false,
))
->add('tel')
->add('birthday', 'birthday')
->add('agreement', 'checkbox', array(
'property_path' => false,
'required' => true,
))
;
$builder->addValidator(new CallbackValidator(function($form) {
if (!$form['agreement']->getData()) {
$form['agreement']->addError(new FormError('利用規約に同意してください'));
}
}));
}

AFTER

use Symfony\Component\Form\FormEvents;

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('email','repeated',array(
'type' => 'email',
'invalid_message' => '同じ値を入力してください',
))
->add('rawPassword','password',array(
'always_empty' => false,
))
->add('tel')
->add('birthday','birthday')
->add('agreement','checkbox',array(
'property_path' => false,
'required' => true,
))
;
$builder->addEventListener(FormEvents::POST_BIND,function($event){
$form = $event->getForm();
if(!$form->has('agreement')){
$form->addError(new FormError('利用規約に同意してください'));
}
});
}

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1