Add constraints in upload image : SonataMediaBundle

烂漫一生 提交于 2019-12-24 07:16:53

问题


How can I add constraints to upload an image, for example : max size, error message, there is not thing about that in the config of sonata_media.

thank you very much.


回答1:


First you will use the CompilerPassInterface component to override the SonataMediaBundle's MediaAdmin class as per link:

Overriding the part of bundle

supose you are in AcmeDemoBundle:

      <?php

       namespace Acme\DemoBundle\DependencyInjection;

       use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
       use Symfony\Component\DependencyInjection\ContainerBuilder;

       class OverrideServiceCompilerPass implements CompilerPassInterface
       {
         public function process(ContainerBuilder $container)
         {
            $definition1 = $container->getDefinition('sonata.media.admin.media');
            $definition1->setClass('Acme\DemoBundle\Admin\MediaAdmin');


          }
      }

Second you will activate your CompilerPassInterface as per the link:

how to activate CompilerPassInterface

  <?php

  namespace Acme\DemoBundle;

  use Symfony\Component\HttpKernel\Bundle\Bundle;
  use Symfony\Component\DependencyInjection\ContainerBuilder;

  use Acme\DemoBundle\DependencyInjection\OverrideServiceCompilerPass;

  class AcmeDemoBundle extends Bundle
   {
   public function build(ContainerBuilder $container)
   {
    parent::build($container);

    $container->addCompilerPass(new OverrideServiceCompilerPass());
    }
  }

and in third and final You will override MediaAdmin class of sonatamediabundle as per the link:

INLINE VALIDATION¶(19.3 USING THE ADMIN CLASS¶)

        <?php

        namespace Acme\DemoBundle\Admin;

        use Sonata\AdminBundle\Admin\Admin;
        use Sonata\AdminBundle\Form\FormMapper;
        use Sonata\AdminBundle\Datagrid\DatagridMapper;
        use Sonata\AdminBundle\Datagrid\ListMapper;
        use Sonata\MediaBundle\Provider\Pool;
        use Sonata\AdminBundle\Route\RouteCollection;
        use Sonata\MediaBundle\Admin\BaseMediaAdmin as BaseAdmin;
        use Sonata\MediaBundle\Form\DataTransformer\ProviderDataTransformer;
        use Sonata\AdminBundle\Validator\ErrorElement;

        class MediaAdmin extends BaseAdmin
         {
            // add this method
            public function validate(ErrorElement $errorElement, $object)
            {
            $errorElement
              ->with('name')
                ->assertMaxLength(array('limit' => 32))
              ->end()
              ->with('description')
                  ->assertNotNull(array())
                   ->assertLength(array('min' => 2,
                                 'max' => 50))
              ->end()

              // remaining field here
                   ;
             }
         }

Now you may validate remaing fields of SonataMediaBundle's Media class located in

      Sonata\MediaBundle\Model\Media

That's all above the need ..




回答2:


I got this issue in my project recently. There is my little hack(symfony 2.3):

use Symfony\Component\Validator\ExecutionContextInterface;

/**
 * @ORM\Entity(repositoryClass="CMS\RequestBundle\Repository\RequestVideoRepository")
 * @ORM\Table(name="request")
 * @Assert\Callback(methods={"isMediaSizeValid"})
 *
 */
class RequestVideo
{

property

/**
 * @ORM\OneToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media",cascade={"persist"})
 */
protected $file;

Validation method

/**
 * @param ExecutionContextInterface $context
 */
public function isMediaSizeValid(ExecutionContextInterface $context)
{
    if($this->getFile() && $this->getFile()->getSize() > 5242880){
        $context->addViolationAt('file', 'File is too big. Please upload file less than 5MB', array(), null);
    }
}

Kinda dirty but I didn't find anything to sort this out. I hope someone will suggest solution better than this.



来源:https://stackoverflow.com/questions/24779143/add-constraints-in-upload-image-sonatamediabundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!