Warning: Missing argument 1 for FOS\UserBundle\Form - php

Im getting this warning and cant seem to find the solution, i have tried various answers but still nothing
Warning: Missing argument 1 for FOS\UserBundle\Form\Type\RegistrationFormType::__construct(), called in C:\xampp\htdocs\symfony-bootstrap\src\sava\UserBundle\Controller\VerUsuarioController.php on line 77 and defined in C:\xampp\htdocs\symfony-bootstrap\vendor\friendsofsymfony\user-bundle\FOS\UserBundle\Form\Type\RegistrationFormType.php on line 25
Notice: Undefined variable: class in C:\xampp\htdocs\symfony-bootstrap\vendor\friendsofsymfony\user-bundle\FOS\UserBundle\Form\Type\RegistrationFormType.php on line 27
my services.yml
# src/Acme/UserBundle/Resources/config/services.yml
services:
sava_user.registration.form.type:
class: sava\UserBundle\Form\RegistroTypes
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: sava_user_registration }
this is my config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: "#savaUserBundle/Resources/config/services.yml" }
framework:
#esi: ~
translator: { fallback: %locale% }
secret: %secret%
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_proxies: ~
session: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
form:
resources:
- 'MopaBootstrapBundle:Form:fields.html.twig'
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
filters:
less:
node: /Arturo/bin/node
node_paths: [/Arturo/lib/node_modules]
# lessphp:
# node: ["C:\\Program Files\\nodejs\\node.exe"]
# node_paths: ["C:\\Users\\Arturo\\AppData\\Roaming\\npm\\node_modules"]
apply_to: "\.less$"
cssrewrite: ~
cssembed:
jar: %kernel.root_dir%/Resources/java/cssembed-0.3.6.jar
apply_to: "\.css$|\.less$"
yui_css:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.6.jar
apply_to: "\.css$"
yui_js:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.6.jar
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: %kernel.root_dir%/data/data.db3
# path: %database_path%
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
spool: { type: memory }
fos_user:
db_driver: orm
firewall_name: main
user_class: sava\UserBundle\Entity\User
registration:
form:
type: sava_user_registration
this is my controlleraction
public function modificarUserAction(Request $request,$id) {
$em = $this->getDoctrine()->getManager();
$User = $em->getRepository('savaUserBundle:User')->find($id);
$formulario = $this->createForm(new \sava\UserBundle\Form\RegistroType(),$User);
$formulario->handleRequest($request);
if ($request->getMethod() == 'POST') {
$em = $this->getDoctrine()->getManager();
$em->persist($User);
$em->flush();
return $this->forward('savaUserBundle:VerUsuario:vertodos');
}
else
{
return $this->render('savaUserBundle:Default:modificarUser.html.twig', array(
'formulario' => $formulario->createView(),'User'=> $User
));
}
its working fine but i keep getting those errors and have no clue in how to get rid of them.

This is a very common question for people who are new to dependency injection containers. You defined your service but then used new RegistroType() to create the instance. The thinking is that php will somehow realize that you want a service and use the container. Nope. The 'new' operator knows nothing about the service container and simply tries to create a new instance without any parameters. Hence the error message.
You obviously read the "defining form type as a service" section (http://symfony.com/doc/current/book/forms.html#defining-your-forms-as-services) and I can see where your confusion comes from. The manual could be a bit clearer perhaps.
Bottom line is that you need to use the form type's alias instead of the new operator:
// Replace
$formulario = $this->createForm(\sava\UserBundle\Form\RegistroType(),$User);
// With
$formulario = $this->createForm('sava_user_registration',$User);
You will get a different error the first time you try this. You have a typo in your service definition but I'll leave that for you to debug.

Related

No route found for all my routes even though they show up in debug:router

Background:
I am trying to learn Symfony3 and was playing around with Internationalization.
Issue:
I broke something and I can't get my routes to work again. The only route that is working is localhost:8000/ (homepage)
What I have at the moment:
config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: "#CarBundle/Resources/config/services.yml" }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
framework:
#esi: ~
translator: { fallbacks: [en] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
handler_id: session.handler.native_file
save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%"
fragments: ~
http_method_override: true
assets: ~
php_errors:
log: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
form_themes:
- 'bootstrap_3_layout.html.twig'
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/../var/data/data.sqlite"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
#path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
car:
resource: "#CarBundle/Controller/"
type: annotation
Example controller (for login)
<?php
/**
* Handles login operations
*/
namespace AppBundle\Controller;
use AppBundle\Entity\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use function dump;
class SecurityController extends Controller
{
/**
* #Route("/login", name="login")
*/
public function loginAction(Request $request)
{
// here we are getting a service from the container
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
}
All my routes
Just to be clear: I have tried bin/console cache:clear and many different configuration options... to no avail...
EDIT 1: The profiler has the following message:
Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /login/" (from "http://localhost:8000/register")" at C:\xampp\htdocs\php\frameworks\symfony\autotrader\var\cache\dev\classes.php line 3487
EDIT 2: New error after removing use function dump;
Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /login/" (from "http://localhost:8000/")" at C:\xampp\htdocs\php\frameworks\symfony\autotrader\var\cache\dev\classes.php line 3487
You have not set /login/ route but only /login route - without last slash in your routes.

Redirecting with a route in Symfony2.8

I'm trying to redirect with a route and I', following the official doc Symfony doc
But I've edited the config.yml file with the route field
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: "#UserBundle/Resources/config/services.yml" }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
translator: { fallbacks: [en] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
#default_locale: "%locale%"
default_locale: es
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
root:
path: /crearUser
defaults:
_controller: UserBundle:Default:create
route: crear
permanent: true
But I get this error
InvalidArgumentException in YamlFileLoader.php line 399: There is no extension able to load the configuration for "root" (in /home/user/pruebasym/app/config/config.yml). Looked for namespace "root", found "framework", "security", "twig", "monolog", "swiftmailer", "doctrine", "sensio_framework_extra", "debug", "web_profiler", "sensio_distribution"
It looks like a missed somenthing but I haven't seen anything diferrent a the official doc
The identation level is not correct, If you want to set it in your config.yml, you must follow this indentation level :
framework:
router:
root:
path: /
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /app
permanent: true
Or simpler, in your routing.yml (like the symfony doc suggests) :
root:
path: /
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /app
permanent: true

Symfony internationalization

I am having a strange problem with translation in Symfony 2.
This is my config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
framework:
#esi: ~
translator: { fallbacks: ['%locale%'] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Assetic Configuration
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: [ ]
#java: /usr/bin/java
filters:
cssrewrite: ~
#closure:
# jar: "%kernel.root_dir%/Resources/java/compiler.jar"
#yui_css:
# jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
mapping_types:
enum: string
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
profile:
form:
type: app_user_profile
parameters.yml
...
locale: ru
And, of course, I have messages.en.yml and messages.ru.yml.
Even when locale is set to 'ru' it displays English text, and $request->getLocale() is always returning 'en'.
What could be the problem and where is it overset to 'en' if not in configs?
The locale parameter in the config.yml file is just a fallback in case a translation for the requested language is not available.
Calling $request->getLocale() will return the language configuration of the browser from which the HTTP-Request was sent.
If you want the translation to be forced into another language take a look at this answer: https://stackoverflow.com/a/14331838/1173391
You need to create a twig extension that allows you to replace ru with en in your path route (and vice versa) :
{# yourProject/app_dev.php/ru #}
Look at this tutorial
http://blog.viison.com/post/15619033835/symfony2-twig-extension-switch-locale-current-route
put in your routing.yml a prefix with the locale like this:
routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
prefix: /{_locale}
requirements:
_locale: "%app.locales%"
config.yml
parameters:
locale: en
app.locales: en|ru
Did you add:
{% trans_default_domain "message" %}
on top of your twig template file where the translation keys exist and did you clear cache?

How to use global variable in symfony twig file?

I have done setting in app/config/config.yml
twig:
globals:
mandatory_note: %mandatory_note%
parameter also set in config.yml file
parameters:
mandatory_note: "Note: * marked fields are mandatory"
And in twig file I have accessed the variable
{{ mandatory_note }}
but still gets error. ie. mandatory_note variable doesnot exists.
This is my config.yml file
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
framework:
#esi: ~
translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
cookie_lifetime: 0
gc_maxlifetime: 36000
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Assetic Configuration
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: []
#java: /usr/bin/java
filters:
cssrewrite: ~
#closure:
# jar: "%kernel.root_dir%/Resources/java/compiler.jar"
#yui_css:
# jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
unix_socket: "/opt/lampp/var/mysql/mysql.sock"
charset: UTF8
mapping_types:
enum: integer
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
encryption: "%mailer_encryption%"
port: "%mailer_port%"
sender_address: "%mailer_sender%"
parameters:
max_contents_per_page: 20
max_pages_per_page: 10
mandatory_note: "Note: * marked fields are mandatory"
twig:
globals:
mandatory_note: %mandatory_note%
You need to put it under Twig. Like that:
twig:
globals:
mandatory_note: %mandatory_note%
Also make sure that mandatory_note is defined in parameters.yml file. To test if global works just use "testString" instead of %mandatory_note%
What if you put them together, like this?
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
globals:
mandatory_note: "%mandatory_note%"
I got solution, I added below code in parameters.yml file:
parameters:
mandatory_note: "Note: * marked fields are mandatory"
twig:
globals:
mandatory_note: "%mandatory_note%"
and it's working finally.

FOSUserBundle : config error

i m using FOSUserBundle i also follow the documentation but i m getting an error
FileLoaderLoadException: Cannot import resource "C:\wamp\www\sym\app/config\config.yml" from "C:\wamp\www\sym\app/config/config_dev.yml".
Not getting whats the error.
app/routing.yml
# Internal routing configuration to handle ESI
#_internal:
# resource: "#FrameworkBundle/Resources/config/routing/internal.xml"
# prefix: /_internal
_user_bundle:
resources:"#AcmeUserBundle/Resources/config/routing.yml"
prefix: /
app/config.yml
imports:
- { resource: parameters.ini }
- { resource: security.yml }
framework:
#esi: ~
translator: { fallback: %locale% }
secret: %secret%
charset: UTF-8
router: { resource: "%kernel.root_dir%/config/routing.yml" }
form: true
csrf_protection: true
validation: { enable_annotations: true }
templating: { engines: ['twig'] } #assets_version: SomeVersionScheme
session:
default_locale: %locale%
auto_start: true
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
# java: /usr/bin/java
filters:
cssrewrite: ~
# closure:
# jar: %kernel.root_dir%/java/compiler.jar
# yui_css:
# jar: %kernel.root_dir%/java/yuicompressor-2.4.2.jar
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
# Swiftmailer Configuration
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
jms_security_extra:
secure_controllers: true
secure_all_services: false
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: Acme\UserBundle\Entity\User
app/config_dev.yml
imports:
- { resource: config.yml }
framework:
router: { resource: "%kernel.root_dir%/config/routing_dev.yml" }
profiler: { only_exceptions: false }
web_profiler:
toolbar: true
intercept_redirects: false
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
firephp:
type: firephp
level: info
assetic:
use_controller: false
You might have included tab in your config.yml or config_dev.yml file. You do not include tab for yml files. You can use space instead of tab. Hope it will help you.

Categories