image - Symfony 4 - Override maxSizeMessage in validator

I try to override the maxSizeMessage atttribute from File in a Symfony 4.2 project however, it is the default message who is returned by the framework ("The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}").

This is my validator configuration for this FormType :

# config\validator\validator.yaml
App\Application\Form\Command\PropertyPicture\PropertyAddPictureCommand:
    properties:
        image: 
            - Image:
                maxSize: 10M
                maxSizeMessage: "Image size over 10M"
                mimeTypes: [image/jpeg, image/png, image/gif, image/bmp]
                mimeTypesMessage: "Image extension not valid"
                uploadErrorMessage: "An error has been encounted during the process"

[EDIT for Michał Tomczuk commentary (2019/04/08)]

This is the "FormType" object in this project (it is a DDD architecture with POMM) :

class PropertyAddPictureCommand
{
    /**
     * @var string
     */
    public $link;

    /**
     * @var string
     */
    public $filename;

    /**
     * @var Property
     */
    public $property;

    /**
     * @var string
     */
    public $pathname;

    /**
     * @var string
     */
    public $directory;

    /**
     * @var UploadedFile
     */
    public $image;
}

[END EDIT for Michał Tomczuk commentary (2019/04/08)]

[EDIT for Michał Tomczuk commentary (2019/04/09)]

This is the FormType object use (the object get before is the values passed in $options array) in the project :

class CommandType extends AbstractType 
{
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'name' => 'command_type',
        ]);
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        foreach (get_object_vars($options['data']) as $key => $value) {
            $builder->add($key);
        }
    }
}

And this is how I call the form object in the controller :

$uploadedFile = $request->files->get('image');
$propertyId = $property->getIdValue();
$directory = 'group'.DIRECTORY_SEPARATOR.$this->getUser()->getGroupId().DIRECTORY_SEPARATOR.'property'.DIRECTORY_SEPARATOR.$propertyId;

$filename = $propertyId.'-'.uniqid().'.'.$uploadedFile->getClientOriginalExtension();

$command = new PropertyAddPictureCommand();
$command->link = $this->getParameter('amazon_s3.bucket_url').'images/'.$directory.DIRECTORY_SEPARATOR.$filename;
$command->filename = $filename;
$command->property = $property;
$command->pathname = $uploadedFile->getPathname();
$command->directory = $directory;

$form = $this->createForm(CommandType::class, $command, ['data_class' => PropertyAddPictureCommand::class]);
$form->submit($request->request->all() + ['image' => $uploadedFile], false);

if (!$form->isValid()) {
    throw new NotValidFormException($form);
}

[END EDIT for Michał Tomczuk commentary (2019/04/09)]

I expected to show "Image size over 10M" message when the image size is over 10M.

1 Answer

  1. Glen- Reply

    2019-11-13

    I think it is a property path issue.

    Your form has a name - command_type, which means that every input name is preceeded with it, so instead of appending image to the request, try appending command_type[image].

    I think you could also simply map the image to the command object:

    $command->image = $uploadedFile;
    

    In this case you don't need to append the image property to the request.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>