Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/09/2015, 05:01
jorgejts
 
Fecha de Ingreso: enero-2015
Ubicación: Valenica (España)
Mensajes: 3
Antigüedad: 9 años, 3 meses
Puntos: 0
Problemas previsualizando una imagen (image preview)

Hola amigos,

Estoy intentando que un formulario me previsualice una imagen al seleccionarla. Lo estoy haciendo siguiendo la traducción del libro oficial de gitnacho ([URL="http://gitnacho.github.io/symfony-docs-es/cookbook/form/create_form_type_extension.html#index-0"]este[/URL]). Mi problema es que cuando intento visualizar el formulario me da este error:

"Attempted to call an undefined method named "set" of class "Symfony\Component\Form\FormView".
Did you mean to call e.g. "offsetExists", "offsetGet", "offsetSet", "offsetUnset" or "setRendered"? "

En el siguiente sitio:

src/AppBundle/Form/Extension/ImageTypeExtension.php at line 52

// configura una variable "image_url" que debe estar disponible al dibujar este campo
$view->set('image_url', $imageUrl);
}
}

Me pregunto si puede ser un error del servicio. Os pongo el código:

El servicio:

Código:
 app_bundle.image_type_extension:
        class: AppBundle\Form\Extension\ImageTypeExtension
        tags:
                - { name: form.type_extension, alias: file }
La clase ImageTypeExtension, que es donde me sale el error:

Código:
<?php
namespace AppBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ImageTypeExtension extends AbstractTypeExtension
{
    /**
     * Devuelve el nombre del tipo que será extendido.
     *
     * @return string The name of the type being extended
     */
    public function getExtendedType()
    {
        return 'file';
    }

    /**
     * Añade la opción image_path
     *
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setOptional(array('image_path'));
    }

    /**
     * Pase la URL de la imagen a la vista
     *
     * @param FormView $view
     * @param FormInterface $form
     * @param array $options
     */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        if (array_key_exists('image_path', $options)) {
            $parentData = $form->getParent()->getData();

            if (null !== $parentData) {
                $accessor = PropertyAccess::getPropertyAccessor();
                $imageUrl = $accessor->getValue($parentData, $options['image_path']);
            } else {
                 $imageUrl = null;
            }

            // configura una variable "image_url" que debe estar disponible al dibujar este campo
            $view->set('image_url', $imageUrl);
        }
    }

}
?>
y la plantilla de twig con el file-widget:

Código:
{# src/AppBundle/Resources/views/Fotos/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}

{% block file_widget %}
    {% spaceless %}

    {{ block('form_widget') }}
    {% if image_url is not null %}
        <img src="{{ asset(image_url) }}"/>
    {% endif %}

    {% endspaceless %}
{% endblock %}
Si hace falta algo más, pedidmelo.

Muchas gracias de ante mano.