diff --git a/Form/Type/AcbFileType.php b/Form/Type/AcbFileType.php index 24b7fde..88dc810 100644 --- a/Form/Type/AcbFileType.php +++ b/Form/Type/AcbFileType.php @@ -19,6 +19,7 @@ use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints\File; +use Symfony\Component\Validator\Constraints\Image; use Symfony\Component\Validator\Constraints\NotBlank; class AcbFileType extends AbstractType @@ -141,6 +142,14 @@ private function updateForm(FormInterface $form, $data, $options, $hasFile = fal } } + $hasImageConstraint = false; + foreach ($options['file_constraints'] as $constraint) { + if ($constraint instanceof Image) { + $hasImageConstraint = true; + break; + } + } + if (false === $hasNotBlankConstraint && true === $options['required'] && false === $isFileUploaded) { $options['file_constraints'][] = new NotBlank(null, null, null, null, $options['validation_groups']); } @@ -157,7 +166,9 @@ private function updateForm(FormInterface $form, $data, $options, $hasFile = fal $mimeTypes = $this->mimeTypeManager->getAllMimeTypes(); } - $options['file_constraints'][] = new File(null, null, null, $mimeTypes); + if (false === $hasImageConstraint) { + $options['file_constraints'][] = new File(null, null, null, $mimeTypes); + } $form ->add('file', FileType::class, [ diff --git a/Form/Type/ImageType.php b/Form/Type/ImageType.php index a6daad9..2c84605 100644 --- a/Form/Type/ImageType.php +++ b/Form/Type/ImageType.php @@ -58,7 +58,7 @@ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'translation_domain' => 'AdvancedContentBundle', - 'file_constraints' => [new Image()], + 'file_constraints' => [new Image(null, null, null, $this->mimeTypeManager->getMimeTypesByCode(MimeTypeManager::MIME_TYPE_IMAGE))], 'mime_types' => array_flip(array_map('ucfirst', $this->mimeTypeManager->getImageMimeTypesChoices())), ]); } diff --git a/Resources/js/index.js b/Resources/js/index.js index 40ebc31..961f655 100644 --- a/Resources/js/index.js +++ b/Resources/js/index.js @@ -263,17 +263,20 @@ jQuery(function ($) { return; } - let mimeTypeOption = $(this).closest('.acb-widget-container').find('[data-mime-type-restriction-values]').find(':checked'); + let mimeTypeOption = $(this).closest('.acb-widget-container').find('[data-mime-type-restriction-values]').find(':selected'); + let mimeTypeValues = []; - if (!mimeTypeOption.length) { - return; + if (mimeTypeOption.length) { + mimeTypeValues = mimeTypeOption.data('mime-type'); + } else { + $(this).closest('.acb-widget-container').find('[data-mime-type-restriction-values]').find('option').each(function() { + mimeTypeValues = $.merge(mimeTypeValues, $(this).data('mime-type')); + }); } - - let mimeTypeValues = []; - mimeTypeOption.each(function() { - $(this).data('mime-type').forEach((element) => mimeTypeValues.push(element)); - }); + if (!mimeTypeValues.length) { + return; + } let allImageType = mimeTypeValues.length === 1 && mimeTypeValues[0] === 'image/*'; let hasError = true;