genemu / GenemuFormBundle

Extra Form : Captcha GD, Tinymce, Recaptcha, JQueryDate, JQueryAutocomplete, JQuerySlider, JQueryFile, JQueryImage
587 stars 265 forks source link

Autocompete with symfony2.7 #412

Open aimeric opened 8 years ago

aimeric commented 8 years ago

Hi,

I have the following error when I implement genemu_jqueryautocompleter_entity.

 Catchable Fatal Error: Argument 1 passed to Genemu\Bundle\FormBundle\Form\Core\DataTransformer\ChoiceToJsonTransformer::__construct() must implement interface Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface, instance of Symfony\Component\Form\ChoiceList\LegacyChoiceListAdapter given, called in /srv/www/Toolkit/vendor/genemu/form-bundle/Genemu/Bundle/FormBundle/Form/JQuery/Type/AutocompleterType.php on line 46 and defined

I fixed the error by editing: LegacyChoiceListAdapter

use Symfony\Component\Form\ChoiceList\LegacyChoiceListAdapter;

  /**
     * Constructs
     *
     * @param ArrayChoiceList $choiceList
     * @param boolean         $ajax
     */
    public function __construct($choiceList, $ajax = false, $widget = 'choice', $multiple = false)
    {
        if ($choiceList instanceof LegacyChoiceListAdapter){
            $this->choiceList = $choiceList->getAdaptedList();
        }
        else if($choiceList instanceof ChoiceListInterface){
            $this->choiceList = $choiceList;
        }

        $this->ajax = $ajax;
        $this->multiple = $multiple;
        $this->widget = $widget;
    }

but now i got a new error:

Notice: Undefined offset: 1 on file in vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php at line 213 

// Add ungrouped items directly
            self::addChoiceView(
                $choices[$value],
                $value,
                $label,
                $keys,

this is my PostType.php

<?php

namespace FLY\BookingsBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class PostType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('airport', 'genemu_jqueryautocompleter_entity', array(
                'route_name' => 'ajax_airport',
                'property' => 'airport',
                'multiple' => true,
                'class' => 'FLY\BookingsBundle\Entity\IataAirportCodes',

            ))

            ->add('departure')
            ->add('arrival')
            ->add('departuredate')
            ->add('arrivaldate')
            ->add('pounds')
            ->add('price')
            ->add('compagny')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FLY\BookingsBundle\Entity\Post'

        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'fly_bookingsbundle_post';
    }
}

Thank you :)

aimeric commented 8 years ago

Any idea ? i'm still stack :(

lealceldeiro commented 8 years ago

Have anyone solved this issue? I'm stuck here too! I'm using Symfony 2.7.7

aimeric commented 8 years ago

Hi lealceldeiro, i didn't found a way to solved my issue. if you want to pull your data from the database this is what you can do : ->add('lealceldeiro', 'genemu_jqueryselect2_entity', array( 'class' => 'MylealceldeiroBundle:lealceldeiro', 'property' => 'lealceldeiro', 'query_builder' => function(EntityRepository $er) { return $er->createQueryBuilder('u') ->groupBy('u.code') ->orderBy('u.lealceldeirot', 'ASC'); },) ) i'm using query_builder to speed up the research because i have more than 10.000 items. then in your twig file you can use select2 it will help you to have a better interface to search an element in your dropdown. https://select2.github.io/

lealceldeiro commented 8 years ago

aimeric thanks for the reaply. I've decided to try something different. Here is something related to what I did (http://stackoverflow.com/questions/32334289/adding-jquery-autocomplete-in-symfony2-entity-field) I don't know yet if this is a better or a worse solution regarding to performance, but at least I got it working properly. I'll try what you told me later.

sensi commented 8 years ago

Issue allready exists. Also in symfony 2.8.x.