Symfony "Could not load type" service yaml - forms

I tried to create a service in Symfony 2.2 for one of my form :
service.yml:
tyg_user.settings.form:
class: Symfony\Component\Form\Form
factory_method: createNamed
factory_service: form.factory
arguments:
- tyg_user_settings
- tyg_user_settings_name
tyg_user.settings.form.type:
class: TyG\UserBundle\Form\Settings\SettingsFormType
tags:
- { name: form.type, alias: tyg_user_settings }
tyg_user.settings.form.handler:
class: TyG\UserBundle\Form\Settings\SettingsFormHandler
scope: request
arguments:
- #tyg_user.settings.form
- #request
- #fos_user.user_manager
SettingsForm.php
<?php
namespace TyG\UserBundle\Form\Settings;
use Symfony\Component\Form\FormBuilderInterface as FormBuilder;;
use Symfony\Component\Form\AbstractType;
class SettingsForm extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('birthday', 'birthday')
->add('email', 'email')
->add('showmail')
->add('showbirthday')
;
}
public function getName()
{
return 'tyg_user_settings';
}
}
?>
But an error occuered :
Could not load type "tyg_user_settings_name
This occurs when I get my form through the service container :
$this->container->get('tyg_user.settings.form');
I used to do create my service through the xml format but when I change to the yml format I can't figure out how to to make it works

The tyg_user_settings_name is referencing a form type alias.
If you are wanting it to reference a parameter you should use %tyg_user_settings_name% instead.

Related

symfony6 $this->container->get('app.our.useful.thing')

I am sorry, I read the docs, but I don't get symfony6 anymore.
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/EventListener
app.our.useful.thing:
class: App\OurUsefulThing
public: true
My src/OurUsefulThing.php:
<?php
namespace App;
class OurUsefulThing
{
public function sayHello()
{
return "Hello Service";
}
}
In the controller:
public function someActionName(Request $request)() {
$value = $this->container->get('app.our.useful.thing')->sayHello();
}
Error Message
Service "app.our.useful.thing" not found: even though it exists in the app's container, the container inside "App\Controller\CategoriesController" is a smaller service locator that only knows about the "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer" and "twig" services. Try using dependency injection instead.
It was in symfony4 so easy. What I am doing wrong?

symfony 3.4 : forms : Use Data Transformers

trying to use data transformer, I have an error when loadding my html form
In my formType class
use Doctrine\ORM\EntityManagerInterface;
class PmpType extends AbstractType
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
}
in my app\config\services.yml
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: true
but I received this error
Type error: Too few arguments to function myApp\Bundle\Form\PmpType::__construct(), 0 passed in \vendor\symfony\symfony\src\Symfony\Component\Form\FormRegistry.php on line 92 and exactly 1 expected
What do I miss
Thx for hepl
OK
I don't understand why but I have toset the service for my class :
in service.yml, I add
seims_pmp.pmptype:
class: SEISM\PmpIG56Bundle\Form\PmpType
arguments: ["#doctrine.orm.entity_manager"]
tags: [form.type]
and now it's working

Symfony 4 argument has no type-hint, you should configure its value explicitly

Symfony 4.2.3
Recently upgraded from version 3.4 to 4.2.3 and got my project working, but
when setting autoconfigure in services.yaml to true, I will receive this error message:
Cannot autowire service "App\EventListener\RedirectToLocaleActiveListener": argument "$localeActive" of method "__construct()" has no type-hint, you should configure its value explicitly.
My services.yaml
parameters:
locale: de
locale_active: de
app_locales: de|en
uploads_directory_name: uploads
uploads_profile_directory_name: profiles
uploads_directory: '%kernel.root_dir%/../public/%uploads_directory_name%'
profile_directory: '%kernel.root_dir%/../public/%uploads_directory_name%/%uploads_profile_directory_name%'
google_recaptcha_site_key: '%env(GOOGLE_RECAPTCHA_SITE_KEY)%'
services:
_defaults:
autowire: true
autoconfigure: true
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: ../src/Controller
tags:
- controller.service_arguments
locales:
class: App\Util\Locales
arguments:
- '%locale_active%'
- '%app_locales%'
- '#session'
app.locale:
class: App\EventListener\LocaleListener
tags:
- {name: kernel.event_subscriber}
app.redirect_to_locale_active:
class: App\EventListener\RedirectToLocaleActiveListener
arguments:
- '#router'
- '%locale_active%'
tags:
- {name: kernel.event_subscriber}
My RedirectToLocaleActiveListener.php
<?php
namespace App\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Class RedirectToLocaleActiveListener
* When a user enters to the homepage without the parameter locale,
* the subscriber redirects the user to the main locale.
*
* #package App\EventListener
*/
class RedirectToLocaleActiveListener implements EventSubscriberInterface
{
/**
* #var UrlGeneratorInterface
*/
private $urlGenerator;
/**
* #var string
*/
private $localeActive;
/**
* #param UrlGeneratorInterface $urlGenerator
* #param $localeActive
*/
public function __construct(UrlGeneratorInterface $urlGenerator, $localeActive)
{
$this->urlGenerator = $urlGenerator;
$this->localeActive = $localeActive;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
/**
* #param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if ('/' == $request->getPathInfo()) {
$route = $this->urlGenerator->generate('app_index', ['_locale' => $this->localeActive]);
$response = new RedirectResponse($route);
$event->setResponse($response);
}
}
}
What I've tried:
adding 'string' to $localActive in __construct of RedirectToLocaleActiveListener
Result:
Cannot autowire service "App\EventListener\RedirectToLocaleActiveListener": argument "$localeActive" of method "__construct()" is type-hinted "string", you should configure its value explicitly.
arguments of scalar type cannot be auto-wired. You need to wire them manually.
You can try wiring the argument explicitly in the service definition:
App\EventListener\RedirectToLocaleActiveListener
arguments:
$urlGenerator: '#router'
$localeActive: '%locale_active%'
tags:
- {name: kernel.event_subscriber}
Documentation:
https://symfony.com/doc/current/service_container.html#manually-wiring-arguments
Or you can make use of the local service binding feature to bind a parameter to a scalar argument:
services:
_defaults:
bind:
$localeActive: '%locale_active%'
Documentation:
https://symfony.com/blog/new-in-symfony-3-4-local-service-binding
If your service name is not equal to fqcn, such as:
app.ext.telegram_bot_api:
class: 'App\Ext\TelegramBot\Bot'
and somewhere your using automatic services resolution like this:
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{Entity,Document,Migrations,Tests,Kernel.php}'
your should create alias between your service name and fqcn like that:
'App\Ext\TelegramBot\Bot': '#app.ext.telegram_bot_api'
so your automatic services resolution should know about your service extra configuration.

Symfony CMF Media Bundle - Could not load type "cmf_media_image"

I want to use the CMF Media Bundle for image uploads. The Bundle was installed successfully.
routing.xml
cmf_media_file:
resource: "#CmfMediaBundle/Resources/config/routing/file.xml"
cmf_media_image:
resource: "#CmfMediaBundle/Resources/config/routing/image.xml"
AppKernel.php
$bundles = array(
...
new Symfony\Cmf\Bundle\MediaBundle\CmfMediaBundle(),
);
Now I want to add the following code to my form:
$builder
-> add('image', 'cmf_media_image', array('required' => false))
;
But I get an error message:
Could not load type "cmf_media_image"
What did i miss?
Maybe you should declare the template like this (in config.yml) :
twig:
form:
resources:
- 'CmfMediaBundle:Form:fields.html.twig'
see http://symfony.com/doc/current/cmf/bundles/media/form_types.html for more info

how to integrate htmlpurifier with symfony2?

I am trying to integrate htmlpurifier into a symfony2 controller, but symfony2 assumes the class I am trying to instantiate is part of that vary controller, but it is not, it is an included class type frmo the htmlpurifier library.
Is there a way to escape the class name so that symfony2 doesn't look for it in the current namespace?
I suggest to use the bundle version of HTMLPurifier for symfony2
you can found it on gitHub : https://github.com/Exercise/HTMLPurifierBundle
it's pretty easy to install with composer
Require the bundle in your composer.json file:
{
"require": {
"exercise/htmlpurifier-bundle": "*",
}
}
Install the bundle:
$ composer update exercise/htmlpurifier-bundle
Register the bundle app/AppKernel.php :
public function registerBundles()
{
return array(
new Exercise\HTMLPurifierBundle\ExerciseHTMLPurifierBundle(),
// ...
);
}
you can use it as a service in a controller :
$purifier = $this->container->get('exercise_html_purifier.default');
$clean_html = $purifier->purify($dirty_html);
or a filter in a twig template :
{{ text|purify }}
also a Form Data Transformer for the symfony2 form builder
it's all in the docs : https://github.com/Exercise/HTMLPurifierBundle
Oh, just found it.
Instead of
require_once dirname('_FILE_') . '/plugins/htmlpurifier/library/HTMLPurifier.auto.php';
$purifier = new HTMLPurifier();
I should put a leading backslash on the class name
require_once dirname('_FILE_') . '/plugins/htmlpurifier/library/HTMLPurifier.auto.php';
$purifier = new \HTMLPurifier();