Created
February 17, 2014 08:50
-
-
Save 77web/9047022 to your computer and use it in GitHub Desktop.
bad example. validation configuration using FormEvent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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