Symfony upgrading 2.1 FOSUserBundle custom registration form Exception - forms

My custom registration form was working well with the version 1.2.* but when i update symfony in 2.1 and FOSUserBundle in 2.0.*, I've got this problem that I don't know how to resolve.
The error :
The type name specified for the service "kairos_user.registration_form_type" does not match the actual name. Expected "kairos_user_registration", given "fos_user_registration"
My service definition :
services:
kairos_user.registration_form_type:
class: Kairos\UserBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: kairos_user_registration }
And my config.yml
fos_user:
db_driver: orm #cf kairos doctrine admin
firewall_name: main
user_class: Kairos\UserBundle\Entity\User
registration:
form:
type: kairos_user_registration

The alias of your registration form service must match the name returned by your registration form type class. In Kairos\UserBundle\Form\Type\RegistrationFormType try to change the return value of getName() method
class RegistrationFormType extends AbstractType
{
// ...
public function getName()
{
return 'kairos_user_registration';
}
}

Related

extending sonata user bundle with more form fields, get Could not load type "Application\Sonata\UserBundle\Form\RegistrationType"

I'm trying to extend the registration form to show more fields, but after trying multiple variations, I think either there's a bug, or the configuration settings I'm seeing on tutorials and posts are not correct for symfony 2.7 it's driving me nuts, thinking maybe wait till I upgrade to version 3.4, but upgrade isn't going smoothly so far.
error -
Could not load type
"Application\Sonata\UserBundle\Form\RegistrationType"
Form -
namespace Application\Sonata\UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array
$options)
{
$builder->add('firstname')
->add('dateOfBirth');
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
// For Symfony 2.x
public function getName()
{
return $this->getBlockPrefix();
}
}
config_dev.yml
fos_user:
db_driver: orm
firewall_name: secured_area
user_class: Application\Sonata\UserBundle\Entity\User
registration:
form:
type: Application\Sonata\UserBundle\RegistrationType
group:
group_class: Application\Sonata\UserBundle\Entity\User
services.yml
services:
app.form.registration:
class: Application\Sonata\UserBundle\Form\RegistrationType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: app_user_registration }
So as mentioned it's symfony 2.7 and Sonata user-bundle 3.2 any help would be appreciated with this one
The part of code where it errors is this line in config_dev.yml
registration:
form:
type: Application\Sonata\UserBundle\RegistrationType
routing.yml
fos_user_register:
resource:
"#FOSUserBundle/Resources/config/routing/registration.xml"
prefix: /register
# sonata_user_register:
# resource:
#
#SonataUserBundle/Resources/config/routing/sonata_registration_1.xml"
# prefix: /register
full config
fos_user:
db_driver: orm # other valid values are
'mongodb', 'couchdb' and 'propel'
firewall_name: secured_area
registration:
form:
type: eventsBundle\Form\RegistrationType
user_class:
Application\Sonata\UserBundle\Entity\User
group:
group_class: Application\Sonata\UserBundle\Entity\User
#group_manager: sonata.user.orm.group_manager
# If you're using doctrine orm (use
Sonata.user.mongodb.group_manager for mongodb)
service:
user_manager: sonata.user.orm.user_manager
# If you're using doctrine orm (use
sonata.user.mongodb.user_manager for mongodb)
If you want to make it simply, you have to override the correct base registration form, from FOS\UserBundle, like the Official FOSUSerBundle documentation :
namespace YourBundle\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
use YourBundle\Entity\User;
class RegistrationType extends BaseType
{
public function __construct()
{
parent::__construct(User::class);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('firstname')
->add('dateOfBirth');
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
// Or for Symfony < 2.8
// return 'fos_user_registration';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
// For Symfony 2.x
public function getName()
{
return $this->getBlockPrefix();
}
}
Then you have to configure your form type as a service :
# app/config/services.yml
services:
app.form.registration:
class: YourBundle\Form\RegistrationType
tags:
- { name: form.type, alias: app_user_registration }
Finally, you must update the configuration of the FOSUserBundle :
# app/config/config.yml
fos_user:
# ...
registration:
form:
type: YourBundle\Form\RegistrationType
# if you are using Symfony < 2.8 you should use the type name instead
# type: app_user_registration
Hope this will helps you...
Nothing from above worked in Symfony 2.7 for me. I upgraded to Symfony 3.4 and it now works!! perfectly following the standard guide https://symfony.com/doc/master/bundles/FOSUserBundle/overriding_forms.html an upgrade really was needed, probably the autowiring features helped.
Try that:
config.yml
registration:
form:
type: eventsBundle\Form\RegistrationType
service.yml
app.form.registration:
class: eventsBundle\Form\RegistrationType
tags:
- { name: form.type, alias: app_user_registration }

Laravel backpack restrict user access to admin panel

I'm trying to change checkIfUserIsAdmin() method in CheckIfAdmin middleware for disabling access to all users without role admin
What happened:
Nothing. backpack_user()->can(...) or backpack_user()->role(...) don't working...
Is that right way to restrict user access to admin panel?
bp - 3.5
laravel - 5.7
php - 7.2
First, create a middleware:
php artisan make:middleware AdminMiddleware
In this file we will check that the user has ‘admin’ role
<?php
namespace App\Http\Middleware;
use Closure;
class AdminMiddleware
{
public function handle($request, Closure $next)
{
if (! \Auth::user()->hasRole('admin'))
return response(trans('backpack::base.unauthorized'),401);
return $next($request);
}
}
Now, add this middleware to /config/backpack/base.php
(don’t delete CheckIfAdmin middleware, just append it)
'middleware_class' => [
\Backpack\Base\app\Http\Middleware\CheckIfAdmin::class,
\App\Http\Middleware\AdminMiddleware::class
],
Offcourse we must cache the config then
php artisan config:cache
One way is to make a Middleware in Http\Middleware like CheckIfAdmin.php with below function.
private function checkIfUserIsAdmin($user)
{
return ($user->is_admin == 1);
}
Then add this middleware in array $routeMiddleware of Http\Kernel.php. Like below.
'admin' => \App\Http\Middleware\checkIfUserIsAdmin::class,

Zend framework: how to pass creation options to services

When registering a service in module.config.php like
'service_manager' => [
'factories' => [
\Path\To\Your\Service\AService => \Path\To\Your\Service\Factory\AServiceFactory,
]
]
I can't pass in creation options when calling the service factory neither in ZF2 (when the factory implements MutableCreationOptionsInterface) nor in ZF3 (via $container->get(\Path\To\Your\Service\AService::class, $options).
Could anyone tell me how to pass the creation options to the services?
MutableOptions is currently only available on plugin manager instances; the service manager does not implement it. This is why you see the discrepancy.
Références : https://github.com/zendframework/zend-servicemanager/issues/7
Sample : https://samsonasik.wordpress.com/2014/08/14/zend-framework-2-using-creationoptions-in-pluginmanager/
COMPLEMENT
My solution is to add a method with a fluent pattern to the AService class :
class AService
{
public function __construct(...)
{
//your code, you can inject variables from $container by AServiceFactory
}
public function setOptions($options)
{
// your setting from $options
...
// fluent pattern
return $this;
}
}
To use your service :
$container->get(\Path\To\Your\Service\Aservice::class)->setOptions($options);

Symfony2 overriding bundle HTML form input constraints

I am trying to override FOSUserBundle's max username length. Seems simple enough but I can't manage to do it.
validation.yml
AppBundle\Entity\User:
properties:
username:
- Length: { max: 5, groups: [CustomRegistration] }
config.yml
fos_user:
[...]
user_class: AppBundle\Entity\User
registration:
form:
validation_groups: [CustomRegistration]
Validation itself works fine. If user provides username longer than 5 characters Symfony shows an error that it should not be longer than 5 characters. The problem is that the HTML form input still uses default FOSUserBundle value (255). Form builder seems to totally ignore validation groups. Is there any way I can tell form builder to use my constraints?
I want to mention that HTML validation works when I use XML format but I need to use YAML and it works only by coincidence so I would not like to rely on such quirk.
I also tried to provide custom type in hope that it will change anything but it didn't. Username input still uses maxlength value of 255. For reference:
getDefaultOptions # AppBundle/Form/RegistrationFormType.php
public function getDefaultOptions(array $options)
{
return [
'data_class' => 'AppBundle\Entity\User',
'validation_groups' => ['Default', 'CustomRegistration']
];
}
config.yml
fos_user:
[...]
user_class: AppBundle\Entity\User
registration:
form:
type: appbundle_registration
services.yml
services:
appbundle.registration.form.type:
class: AppBundle\Form\RegistrationFormType
tags:
- { name: form.type, alias: appbundle_registration }
You should add max-length HTML attribute manually to the field.
Validation has no effect on HTML attributes.
buildForm # AppBundle\Form\RegistrationFormType
$builder->add("username", "text", array("attr" => array("maxlength" => "5")));
See: http://symfony.com/doc/current/reference/forms/types/text.html#max-length
If it doesnt solve the issue, take a look at your template. It can be setted there too..

Symfony2 - how to extend a vendor bundle (e.g. of FOSFacebookBundle)

Scenario:
I'm using a bundle (FOSFacebookBundle) that allows me to set parameters for exactly one facebook app in my configuration. Everything works perfectly fine, but now I need to set not only one app, but multiple.
My approach:
I've created a AcmeFacebookBundle, which allows multiple apps to be defined (configuration defined in Acme\FacebookBundle\DependencyInjection\Configuration) in an array like so:
acme_facebook:
apps:
some_competition:
server_url: %acme.facebook.some_competition.server_url%
file: %kernel.root_dir%/../vendor/facebook/php-sdk/src/base_facebook.php
alias: facebook
app_id: %acme.facebook.some_competition.app_id%
secret: % acme .facebook.some_competition.secret%
cookie: true
permissions: [email, user_birthday, user_location]
some_other_competition:
server_url: %acme.facebook. some_other_competition.server_url%
file: %kernel.root_dir%/../vendor/facebook/php-sdk/src/base_facebook.php
alias: facebook
app_id: %acme.facebook. some_other_competition.app_id%
secret: % acme .facebook. some_other_competition.secret%
cookie: true
permissions: [email, user_birthday, user_location]
In Acme\FacebookBundle\DependencyInjection\AcmeFacebookExtension I am then looping through all apps. The idea is to compare the server_url parameter against the current URL and override the fos_facebook configuration with mine.
class AcmeFacebookExtension extends Extension
{
...
/**
* {#inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
foreach ($config['apps'] as $app)
{
// check for matching path here?
foreach (array('file', 'app_id', 'secret', 'cookie', 'domain', 'logging', 'culture', 'permissions') as $attribute)
{
$container->setParameter('fos_facebook.' . $attribute, $app[$attribute]);
}
}
}
Problem:
But this is exactly where I'm stuck. Obviously, I have no access to the Request object or the DiC from within AcmeFacebookExtension to do this comparison.
Am I going completely wrong in my approach? Do you have any better idea on how to tackle this problem?
What you want to create is a CompilerPass so that you can manipulate the Container after all other configuration has loaded. These should get you started:
Symfony2: Service Container Compiler Passes
Symfony2: Manipulating Service Parameters and Definitions
Create a CompilerPass