Skip to content

Commit

Permalink
Merge pull request #300 from sherlockode/fix/contrainte-without-selec…
Browse files Browse the repository at this point in the history
…tion

Improve constraints validation without selection
  • Loading branch information
Vowow authored Nov 24, 2023
2 parents 11b7c2d + 0ac477f commit bebe256
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
13 changes: 12 additions & 1 deletion Form/Type/AcbFileType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']);
}
Expand All @@ -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, [
Expand Down
2 changes: 1 addition & 1 deletion Form/Type/ImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
]);
}
Expand Down
19 changes: 11 additions & 8 deletions Resources/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bebe256

Please sign in to comment.