Closed RodolVelasco closed 10 years ago
Can you be more specific, what do not worked?
Some tricks:
I edited the original post
What do you mean by "couldnt make it work" ?
No form generation ? No CSS ? No JS ?
When you said "couldnt make it work", is that you got aPHP exception or you get the form without Select2?
This is how It looks
This is my controller
public function genemuAction()
{
$defaultData = array('message' => 'Type your message here');
$form = $this->createFormBuilder($defaultData)
->add('choice', 'genemu_jqueryselect2_choice',
array(
'choices' => array(
'foo' => 'Foo',
'bar' => 'Bar',
'ita' => 'Italia',
'bra' => 'Brasil',
'hol' => 'Holanda',
'ale' => 'Alemania',
'chi' => 'Chile',
)
)
)
->getForm();
return $this->render('AcmeDemoBundle:Welcome:genemu.html.twig', array(
'form' => $form->createView(),));
}
This is my twig
{% extends 'AcmeDemoBundle::layout.html.twig' %}
{% block title %}Symfony - Welcome{% endblock %}
{% block content_header '' %}
{% block content %}
<h1 class="title">Welcome!</h1>
<p>Genemu</p>
<p>
{{ form_start(form) }}
{{ form_end(form) }}
</p>
{% endblock %}
What am I missing?
If you have this result, it means you did not include Select2's js file. Go on http://ivaynberg.github.io/select2/index.html, get the last version of the librairy and add it to your project.
Remember, this bundle do not include any js librairy, it only provide interface to use them with Symfony2.
Do not forget to include form javascripts too!
{% block javascripts %}
{{ parent() }} {% if you already have js on parent template %}
{{ form_javascript(form) }}
{% endblock %}
Thanks guys!! I had to download libraries from the select2 github site. Then added some code to the layout and the twig. This is how it works.
My Controller
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class WelcomeController extends Controller
{
public function indexAction()
{
/*
* The action's view can be rendered using render() method
* or @Template annotation as demonstrated in DemoController.
*
*/
return $this->render('AcmeDemoBundle:Welcome:index.html.twig');
}
public function genemuAction()
{
$defaultData = array('message' => 'Type your message here');
$form = $this->createFormBuilder($defaultData)
->add('autocomplete_example', 'genemu_jqueryselect2_choice',
array(
'choices' => array(
'foo' => 'Foo',
'bar' => 'Bar',
'ita' => 'Italia',
'bra' => 'Brasil',
'hol' => 'Holanda',
'ale' => 'Alemania',
'chi' => 'Chile',
),
'attr' => array(
'class' => 'this_is_not_required',
'style' => 'width: 550px;'
),
)
)
->getForm();
return $this->render('AcmeDemoBundle:Welcome:genemu.html.twig', array(
'form' => $form->createView(),));
}
}
Default symfony2 layout
{% extends "TwigBundle::layout.html.twig" %}
{% block head %}
<link rel="icon" sizes="16x16" href="{{ asset('favicon.ico') }}" />
<link rel="stylesheet" href="{{ asset('bundles/acmedemo/css/demo.css') }}" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
{% endblock %}
{% block title 'Demo Bundle' %}
{% block body %}
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="flash-message">
<em>Notice</em>: {{ flashMessage }}
</div>
{% endfor %}
{% block content_header %}
<ul id="menu">
{% block content_header_more %}
<li><a href="{{ path('_demo') }}">Demo Home</a></li>
{% endblock %}
</ul>
<div style="clear: both"></div>
{% endblock %}
<div class="block">
{% block content %}{% endblock %}
</div>
{% if code is defined %}
<h2>Code behind this page</h2>
<div class="block">
<div class="symfony-content">{{ code|raw }}</div>
</div>
{% endif %}
{% endblock %}
My html twig
{% extends 'AcmeDemoBundle::layout.html.twig' %}
{% block head %}
{{ parent() }}
<link href="{{ asset('bundles/genemuform/select2-3.5.0/select2.css') }}" rel="stylesheet"/>
<script src="{{ asset('bundles/genemuform/select2-3.5.0/select2.js') }}"></script>
<script>
$(document).ready(function() { $("#form_autocomplete_example").select2(); });
</script>
{% endblock %}
{% block title %}Symfony - Welcome{% endblock %}
{% block content_header '' %}
{% block content %}
<h1 class="title">Welcome!</h1>
<p>Genemu</p>
<p>
{{ form_start(form) }}
{{ form_end(form) }}
</p>
{% endblock %}
This is a pic http://prntscr.com/3yk5w4
Thanks again!!!
You are close to use correctly the bundle. In your example, you wrote
<script>
$(document).ready(function() { $("#form_autocomplete_example").select2(); });
</script>
But in this case, if you change something in your form builder, you have to update your JS code too. If you use the code @Soullivaneuh tell you {{ form_javascript(form) }}
, this will generate automaticaly the code wrote manualy.
Agree with @WedgeSama, if you put the code manually, you loose all the interest of this bundle. ;)
I addded {{ form_javascript(form) }}
but nothing happened. This is how it looks right now http://prntscr.com/3zff3j
Didnt work with form_javascript and form_stylesheet. Heres my code
My layout.html.twig
{% extends "TwigBundle::layout.html.twig" %}
{% block head %}
<link rel="icon" sizes="16x16" href="{{ asset('favicon.ico') }}" />
{% endblock %}
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('bundles/acmedemo/css/demo.css') }}" />
<link href="{{ asset('bundles/genemuform/select2-3.5.0/select2.css') }}" rel="stylesheet"/>
{# <!--
<link href="{{ asset('css/ui-lightness/jquery-ui-1.8.16.custom.css') }}" rel="stylesheet" />
<link href="{{ asset('css/uploadify/uploadify.css') }}" rel="stylesheet" />
<link href="{{ asset('css/jcrop/jquery.Jcrop.css') }}" rel="stylesheet" />
<link href="{{ asset('css/tokeninput/token-input-facebook.css') }}" rel="stylesheet" />
--> #}
{{ form_stylesheet(form) }}
{% endblock %}
{% block javascripts %}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="{{ asset('bundles/genemuform/select2-3.5.0/select2.js') }}"></script>
{# <!--
<script src="{{ asset('js/jquery-1.7.min.js') }}"></script>
<script src="{{ asset('js/jquery-ui-1.8.16.custom.min.js') }}"></script>
<script src="{{ asset('tinymce/tiny_mce.js') }}"></script>
<script src="{{ asset('js/i18n/jquery-ui-i18n.js') }}"></script>
<script src="{{ asset('js/uploadify/jquery.uploadify.v2.1.4.min.js') }}"></script>
<script src="{{ asset('js/uploadify/swfobject.js') }}"></script>
<script src="{{ asset('js/jquery.Jcrop.min.js') }}"></script>
You have to apply the fix https://github.com/loopj/jquery-tokeninput/pull/172/files
for tokeninput to get it work!!
<script src="{{ asset('js/jquery/jquery.tokeninput.js') }}"></script>
--> #}
{{ form_javascript(form) }}
{% endblock %}
{% block title 'Demo Bundle' %}
{% block body %}
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="flash-message">
<em>Notice</em>: {{ flashMessage }}
</div>
{% endfor %}
{% block content_header %}
<ul id="menu">
{% block content_header_more %}
<li><a href="{{ path('_demo') }}">Demo Home</a></li>
{% endblock %}
</ul>
<div style="clear: both"></div>
{% endblock %}
<div class="block">
{% block content %}{% endblock %}
</div>
{% if code is defined %}
<h2>Code behind this page</h2>
<div class="block">
<div class="symfony-content">{{ code|raw }}</div>
</div>
{% endif %}
{% endblock %}
My genemu.html.twig
{% extends 'AcmeDemoBundle::layout.html.twig' %}
{#
{% block head %}
{{ parent() }}
<script>
//$(document).ready(function() { $("#form_autocomplete_example").select2(); });
</script>
{% endblock %}
#}
{% block title %}Symfony - Welcome{% endblock %}
{% block content_header '' %}
{% block content %}
<h1 class="title">Welcome!</h1>
<p>Genemu</p>
<p>
{{ form_widget(form) }}
</p>
{% endblock %}
Have you add select2 to your config.yml?
genemu_form:
select2: ~
I have this config file in xml version. How can I change it to yml? Or how can I do this configuration for xml?
edited Does this line
<service id="genemu.form.jquery.type.select2" class="Genemu\Bundle\FormBundle\Form\JQuery\Type\Select2Type" abstract="true" />
is the equivalent of
genemu_form:
select2: ~
Normaly, the equivalent is that, but not sure, I always use yaml instead of xml.
<genemu_form:config select2>
</genemu_form:config>
Just to be sure, when I talk about config.yml
(or config.xml
), I mean the one in app/config
.
Thanks @WedgeSama . Now I think I could make it work.
My layout is
{% extends "TwigBundle::layout.html.twig" %}
{% block head %}
<link rel="icon" sizes="16x16" href="{{ asset('favicon.ico') }}" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="{{ asset('bundles/genemuform/select2-3.5.0/select2.css') }}" rel="stylesheet"/>
<script src="{{ asset('bundles/genemuform/select2-3.5.0/select2.js') }}"></script>
{% endblock %}
{% block title 'Demo Bundle' %}
{% block body %}
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="flash-message">
<em>Notice</em>: {{ flashMessage }}
</div>
{% endfor %}
{% block content_header %}
<ul id="menu">
{% block content_header_more %}
<li><a href="{{ path('_demo') }}">Demo Home</a></li>
{% endblock %}
</ul>
<div style="clear: both"></div>
{% endblock %}
<div class="block">
{% block content %}{% endblock %}
</div>
{% if code is defined %}
<h2>Code behind this page</h2>
<div class="block">
<div class="symfony-content">{{ code|raw }}</div>
</div>
{% endif %}
{% endblock %}
And my genemu.html.twig is
{% extends 'AcmeDemoBundle::layout2.html.twig' %}
{% block head %}
{{ parent() }}
{{ form_javascript(form) }}
{% endblock %}
{% block title %}Symfony - Welcome{% endblock %}
{% block content_header '' %}
{% block content %}
<h1 class="title">Welcome!</h1>
<p>Genemu</p>
<p>
{{ form_widget(form) }}
</p>
{% endblock %}
And in my app/config.yml I added this
genemu_form:
select2: ~
Maybe with more examples ....
Edited on 27-June-2014
My AppKernel
My composer.json
My app/config.yml
In my controller I just pasted this and also trying different combinations