[英]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. 我正在尝试扩展注册表单以显示更多字段,但是尝试了多种变体之后,我认为可能是错误,或者我在教程和帖子中看到的配置设置不适用于symfony 2.7,这让我发疯了,我想可能要等到我升级到3.4版,但到目前为止升级进展并不顺利。
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 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.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 因此,如前所述,它是symfony 2.7和Sonata用户捆绑3.2版,对此提供了任何帮助
The part of code where it errors is this line in config_dev.yml 错误的代码部分是config_dev.yml中的这一行
registration:
form:
type: Application\Sonata\UserBundle\RegistrationType
routing.yml 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 #前缀:/ 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 : 如果要简单地做,则必须覆盖
FOS\\UserBundle
的正确基本注册表格,例如FOSUSerBundle官方文档 :
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
: 最后,您必须更新
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!!
我升级到了Symfony 3.4,现在可以使用了!! 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.
完全遵循标准指南https://symfony.com/doc/master/bundles/FOSUserBundle/overriding_forms.html确实需要升级,可能是自动装配功能有所帮助。
Try that: 试试看:
config.yml 配置文件
registration:
form:
type: eventsBundle\Form\RegistrationType
service.yml service.yml
app.form.registration:
class: eventsBundle\Form\RegistrationType
tags:
- { name: form.type, alias: app_user_registration }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.