【问题标题】:SonataUserBundle override form profileSonataUserBundle 覆盖表单配置文件
【发布时间】:2013-06-21 04:44:03
【问题描述】:

我正在使用 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:运行时通知:声明 Application\Sonata\UserBundle\Form\Handler\ProfileFormHandler::process() 应该兼容 Sonata\UserBundle\Form\Handler\ProfileFormHandler::process(FOS\UserBundle\Model\UserInterface $用户)在 D:\xampp\htdocs\misplanes.dev\src\Application\Sonata\UserBundle\Form\Handler\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”。

我真的不知道错误在哪里,我尝试了很多配置,我也阅读了不同的论坛和博客,但我没有找到解决方案。我将非常感谢您的帮助。谢谢

【问题讨论】:

    标签: symfony sonata-admin symfony-sonata sonata-user-bundle


    【解决方案1】:

    我终于找到了解决办法。在上面的代码中我有各种错误。

    1. ProfileType.php 是可以的,但是为了避免冲突,我更改了 GetName() 中的返回参数,所以,代码在这里

      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';
          }
      }
      
    2. 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);
          }
      }
      
    3. 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
      
    4. 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) }} 
    

    附言。对不起我的英语水平xD..

    【讨论】:

    • 感谢您发布此信息!它帮助我相对轻松地弄清楚了这个过程。
    • 我的图片在 onetoone 上,但提交表单时出错。图像从 app.user .. 当前用户会话中消失。同时编辑当前用户。
    猜你喜欢
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 2014-05-22
    • 2012-09-19
    • 2016-03-06
    • 2018-11-29
    相关资源
    最近更新 更多