Skip to content

Commit

Permalink
add file error upload checker
Browse files Browse the repository at this point in the history
  • Loading branch information
liverbool committed Jul 26, 2018
1 parent b1e3fdc commit 88fdcf9
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Form/Type/ImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Ishmael Doss <nukboon@gmail.com>
Expand Down Expand Up @@ -92,6 +95,16 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'required' => false,
])
;

if ($options['check_file_error']) {
$builder->get('file')->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$file = $event->getData();

if ($file && $file instanceof UploadedFile && false === $file->isValid()) {
$event->getForm()->addError(new FormError($file->getErrorMessage()));
}
});
}
}

/**
Expand All @@ -108,6 +121,17 @@ public function convertBase64ImageListener(FormEvent $event): void
}
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
parent::configureOptions($resolver);

// should override with your own validator
$resolver->setDefault('check_file_error', true);
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 88fdcf9

Please # to comment.