Skip to content

Instantly share code, notes, and snippets.

@77web
Created February 17, 2014 08:50
Show Gist options
  • Save 77web/9047022 to your computer and use it in GitHub Desktop.
Save 77web/9047022 to your computer and use it in GitHub Desktop.
bad example. validation configuration using FormEvent
<?php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class FileUploadType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$basePath = $options['base_path'];
$builder
->add('folder')
->add('file', 'file')
->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($basePath)
{
/** @var \Symfony\Component\Form\FormInterface $form */
$form = $event->getForm();
$data = $event->getData();
if (file_exists($bathPath.'/'.$data['folder'].'/'.$data['file'])) {
$form['file']->addError(new FormError('ファイル名は既に使われています。'));
}
})
;
}
public function getName()
{
return 'file_upload';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment