2

我正在使用 SonataUserBundle,我正在尝试覆盖编辑配置文件表单,但我不确定 services.yml 和 config.yml。这是代码。

配置文件类型.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;
    }
}

服务.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

配置.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 $user) 兼容在 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”。

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

4

1 回答 1

9

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

  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. 服务.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. 配置.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..

于 2013-06-27T15:15:06.547 回答