Problem loading image of an entity

Asked

Viewed 96 times

0

I have an icon manager, in it I have already made available all the crud.

However I have a problem, when opening the form, a Zend\Form to edit an icon I cannot load the image that is already saved.

I’m using the following element:

  $element = new Image();
    $element->setName('my-image')
            ->setAttribute('src', '/assets/no_image.png')
            ->setAttribute('id','my-image')
            ->setAttribute('height', '96')                
            ->setAttribute('width','96')
            ->setAttribute("disabled", "disabled");
    $this->add($element);

Could someone help me?

  • That would be a input type="file" ?

  • I have an input type="file" for the upload, this element I posted is where the image that is already saved should be shown. At least it was closer to what I got.

  • Are you displaying that image icon not found? I think the problem is in src. Try taking the first bar off

  • The image that is in src appears normally. The problem is to load the image that comes from the database: it never appears, only the one that is in src that displayed

1 answer

0

Use the Serverurl helper as in the example below:

Defining form:

use Zend\Form\Form;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\Form\Element\Text;
use Zend\Form\Element\Image;

class Perfil extends Form
{
  use ServiceLocatorAwareTrait;

  public function init() {
    // Obtendo o helper ServerUrl
    $serverUrl = $this->getServiceLocator()
    ->get('ViewHelperManager')
    ->get('ServerUrl');

    // Criando elementos do formulário
    $name = new Text();
    $name->setLabel('Nome')
    ->setName('name');

    $avatar = new Image();
    $avatar->setLabel('Imagem')
    ->setName('avatar')
    ->setAttribute('src', $serverUrl->__invoke('/images/100x100.gif'));

    $this->add($name)
    ->add($avatar);
  }
}

Using the form in a controller instantiating the form:

<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Form\Perfil;

class IndexController extends AbstractActionController
{

  public function indexAction()
  {
    $form = new Perfil();
    $form->setServiceLocator($this->getServiceLocator());
    $form->init();

    return new ViewModel(array(
      'form' => $form
    ));
  }
}

Can also be used by making dependency injection by the Module class

<?php

namespace Application;

use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Application\Form\Perfil;

class Module implements ServiceProviderInterface
{

  public function getServiceConfig()
  {
    return array(
      'factories' => array(
        'Application\Form\Perfil' => function (ServiceLocatorInterface $serviceLocator) {
          $form = new Perfil();
          $form->setServiceLocator($serviceLocator);
          $form->init();

          return $form;
        }
      )
    );
  }

}

Calling the service created in Module.php in your controller:

<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Form\Perfil;

class IndexController extends AbstractActionController
{

  public function indexAction()
  {
    $form = $this->getServiceLocator()->get('Application\Form\Perfil');

    return new ViewModel(array(
      'form' => $form
    ));
  }
}

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.