symfony2 chained selectors

OK, I finally figured out how to do it properly: namespace Test\TestBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; use Doctrine\ORM\EntityRepository; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\Event\DataEvent; use Test\TestBundle\Entity\Country; use Test\TestBundle\Entity\State; use Test\TestBundle\Entity\City; class CityType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder->add(‘name’); $factory = $builder->getFormFactory(); $refreshStates = function ($form, $country) use ($factory) { $form->add($factory->createNamed(‘entity’,’state’, null, array( … Read more

Symfony2 default locale in routing

If someone is interested in, I succeeded to put a prefix on my routing.yml without using other bundles. So now, thoses URLs work : www.example.com/ www.example.com//home/ www.example.com/fr/home/ www.example.com/en/home/ Edit your app/config/routing.yml: ex_example: resource: “@ExExampleBundle/Resources/config/routing.yml” prefix: /{_locale} requirements: _locale: |fr|en # put a pipe “|” first Then, in you app/config/parameters.yml, you have to set up a … Read more

How to give container as argument to services

It’s easy, if service extends ContainerAware use \Symfony\Component\DependencyInjection\ContainerAware; class YouService extends ContainerAware { public function someMethod() { $this->container->get(‘router’)->generate(‘fos_user_profile_edit’) … } } service.yml your.service: class: App\…\YouService calls: – [ setContainer,[ @service_container ] ]

How to access a different controller from inside a controller Symfony2

If you don’t want to define the class as a service, as it doesn’t feel as a good practice to me and @Qoop quoted Fabien saying the same, you can use forwarding: http://symfony.com/doc/current/controller/forwarding.html public function indexAction($name) { $response = $this->forward(‘AcmeHelloBundle:Hello:fancy’, array( ‘name’ => $name, ‘color’ => ‘green’, )); // … further modify the response or … Read more

Is there any sort of “pre login” event or similar?

So, there’s no ‘official’ pre-login event. But thankfully it’s not hard to set one up since Symfony2 is so extendable. The trick is to use your own service to handle authentication. Symfony uses this class when using a login form: Symfony\Component\Security\Http\Firewall\UsernamePasswordFormAuthenticationListener If you override the security.authentication.listener.form.class parameter (originally defined in Symfony\Bundle\SecurityBundle\Resources\config\security_listeners.xml) you can use a … Read more

How to render content from string/database with twig? [duplicate]

see http://twig.sensiolabs.org/doc/functions/template_from_string.html and http://symfony.com/doc/current/cookbook/templating/twig_extension.html#register-an-extension-as-a-service {% include template_from_string(“Hello {{ name }}”) %} {% include template_from_string(page.template) %} Since the string loader is not loaded by default, you need to add it to your config. # src/Acme/DemoBundle/Resources/config/services.yml acme.twig.extension.loader: class: Twig_Extension_StringLoader tags: – { name: ‘twig.extension’ } Where Acme/acme is your application name and DemoBundle is the bundle you … Read more

Symfony Forms: HTML5 datalist

First, add your new FormType for the field:. <?php // src/Acme/Form/Type/DatalistType namespace Acme\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; class DatalistType extends AbstractType { public function getParent() { return TextType::class; } public function configureOptions(OptionsResolver $resolver) { $resolver->setRequired([‘choices’]); } public function buildView(FormView $view, FormInterface $form, array $options) { $view->vars[‘choices’] = $options[‘choices’]; } … Read more

Use custom delimiters in the current Twig template

As you can see, it’s not recommanded to use this feature Customizing the Syntax BTW here’s a quick and easy example to explain how to use custom delimiters in symfony: service.yml services: templating_lexer: public: true parent: templating.engine.twig class: Acme\YourBundle\Twig\TwigLexerEngine TwigLexerEngine namespace Acme\YourBundle\Twig; use Symfony\Bundle\TwigBundle\TwigEngine; class TwigLexerEngine extends TwigEngine { public function setTwigLexer($lexer) { $this->environment->setLexer($lexer); return … Read more

org.openqa.selenium.NoSuchSessionException: Unable to find session with ID error testing with Behat/Mink and Selenium2Driver in docker container

This error message… org.openqa.selenium.NoSuchSessionException: Unable to find session with ID: url\n Build info: version: ‘4.0.0-alpha-6’, revision: ‘5f43a29cfc’\n System info: host: ‘fca78c7f81e6’, ip: ‘172.28.0.3’, os.name: ‘Linux’, os.arch: ‘amd64’, os.version: ‘5.4.0-42-generic’, java.version: ‘1.8.0_252’\n Driver info: driver.version: unknown …implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session which gets reflected in … Read more