我正在使用SonataUserBundle,我试图覆盖编辑配置文件表单,但我不确定services.yml和config.yml。这是代码。
ProfileType.php
namespace Application\Sonata\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use Sonata\UserBundle\Form\Type\ProfileType as BaseType;
class ProfileType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('ciudad', null, array('label' => 'Ciudad'));
$builder->add('file', 'file', array('required' => false, 'label' => 'Subir Foto'));
}
public function getName()
{
return 'sonata_user_profile';
}
}
ProfileFormHandler.php
<?php
namespace Application\Sonata\UserBundle\Form\Handler;
use Sonata\UserBundle\Model\UserInterface;
use Sonata\UserBundle\Form\Handler\ProfileFormHandler as BaseHandler;
class ProfileFormHandler extends BaseHandler
{
public function process(UserInterface $user)
{
$this->form->setData($user);
if ('POST' == $this->request->getMethod()) {
$this->form->bindRequest($this->request);
if ($this->form->isValid()) {
$nombreArchivoFoto = uniqid().$user->getId() . '-' . $user->getUsername() . '-foto-perfil.jpg';
$user->upload($nombreArchivoFoto);
$this->onSuccess($user);
return true;
}
$this->userManager->reloadUser($user);
}
return false;
}
}
services.yml
services:
sonata_user.registration.form.type:
class: Application\Sonata\UserBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: sonata_user_registration }
sonata_user.profile.form.type:
class: Application\Sonata\UserBundle\Form\Type\ProfileType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: sonata_user_profile }
sonata_user.form.handler.profile:
class: Application\Sonata\UserBundle\Form\Handler\ProfileFormHandler
arguments: ["@fos_user.profile.form", "@request", "@fos_user.user_manager"]
scope: request
public: false
Config.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: Application\Sonata\UserBundle\Entity\User
registration:
form:
type: application_sonata_user_registration
profile:
form:
type: fos_user_profile
handler: fos_user.profile.form.handler.default
name: fos_user_profile_form
validation_groups: [Authentication]
sonata_user:
security_acl: false
class:
user: Application\Sonata\UserBundle\Entity\User
group: Application\Sonata\UserBundle\Entity\Group
profile:
form:
type: sonata_user_profile
handler: sonata_user.form.handler.profile
name: sonata_user_profile_form
validation_groups: [Profile]
如果我使用上述设置,我会得到下一个异常
ErrorException:运行时通知:声明 应用\索纳塔\ UserBundle \表格\处理器\ ProfileFormHandler ::过程() 应该兼容 索纳塔\ UserBundle \表格\处理器\ ProfileFormHandler ::过程(FOS \ UserBundle \型号\的UserInterface $ user)in d:\ XAMPP \ htdocs中\ misplanes.dev的\ src \应用\索纳塔\ UserBundle \表格\处理器\ ProfileFormHandler.php 第8行
如果我改变了services.yml
arguments: ["@sonata_user.profile.form", "@request", "@fos_user.user_manager"]
而不是
arguments: ["@fos_user.profile.form", "@request", "@fos_user.user_manager"]
我得到了下一个异常
ServiceNotFoundException:该服务 “sonata.user.profile.form.handler”依赖于不存在的 服务“sonata_user.profile.form”。
我真的不知道错误在哪里,我已经尝试了很多配置,我已经阅读了不同的论坛和博客,但我还没有找到解决方案。我将非常感谢你的帮助。感谢
答案 0 :(得分:9)
我终于找到了解决方案。在上面的代码中,我遇到了各种错误。
ProfileType.php没问题,但我更改了GetName()中的return参数只是为了避免冲突,因此,代码就在这里
namespace Application\Sonata\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use Sonata\UserBundle\Form\Type\ProfileType as BaseType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ProfileType extends BaseType
{
private $class;
/**
* @param string $class The User class name
*/
public function __construct($class)
{
$this->class = $class;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('ciudad', null, array('label' => 'Ciudad'));
$builder->add('file', 'file', array('required' => false, 'label' => 'Subir Foto'));
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => $this->class
));
}
public function getName()
{
return 'application_sonata_user_profile';
}
}
ProfileFormHandler.php:我在uses语句中发现了一个错误,所以,正确的代码是......
use FOS\UserBundle\Model\UserInterface;
use Sonata\UserBundle\Form\Handler\ProfileFormHandler as BaseHandler;
class ProfileFormHandler extends BaseHandler
{
public function process(UserInterface $user)
{
$this->form->setData($user);
if ('POST' == $this->request->getMethod()) {
$this->form->bindRequest($this->request);
if ($this->form->isValid()) {
$nombreArchivoFoto = uniqid().$user->getId() . '-' . $user->getUsername() . '-foto-perfil.jpg';
$user->upload($nombreArchivoFoto);
$this->onSuccess($user);
return true;
}
$this->userManager->reloadUser($user);
}
return false;
}
protected function onSuccess(UserInterface $user)
{
$this->userManager->updateUser($user);
}
}
Services.yml:
services:
sonata_user.registration.form.type:
class: Application\Sonata\UserBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: sonata_user_registration }
sonata_user.profile.form.type:
class: Application\Sonata\UserBundle\Form\Type\ProfileType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: application_sonata_user_profile }
sonata_user.form.handler.profile:
class: Application\Sonata\UserBundle\Form\Handler\ProfileFormHandler
arguments: ["@sonata.user.profile.form", "@request", "@fos_user.user_manager"]
scope: request
public: false
Config.yml:
fos_user:
db_driver: orm
firewall_name: main
user_class: Application\Sonata\UserBundle\Entity\User
registration:
form:
type: application_sonata_user_registration
profile:
form:
type: fos_user_profile
handler: fos_user.profile.form.handler.default
name: fos_user_profile_form
validation_groups: [Authentication]
sonata_user:
security_acl: false
class:
user: Application\Sonata\UserBundle\Entity\User
group: Application\Sonata\UserBundle\Entity\Group
profile: # Profile Form (firstname, lastname, etc ...)
form:
type: application_sonata_user_profile
handler: sonata_user.form.handler.profile
name: sonata_user_profile_form
validation_groups: [Profile]
最后,另一个错误与模板有关,我使用{{ form_rest(form) }}
来查看新字段,但我不知道为什么这不起作用,所以我不得不把字段放在:
{{ form_label(form.ciudad, 'CIUDAD') }}
{{ form_errors(form.ciudad) }}
{{ form_widget(form.ciudad) }}
PS。对不起我的英语水平xD ..