SonataUserBundle 覆盖表单配置文件 [英] SonataUserBundle override form profile

查看:35
本文介绍了SonataUserBundle 覆盖表单配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using SonataUserBundle and I am trying to override the edit profile form, but I am not sure about the services.yml and the config.yml. Here is the code.

ProfileType.php

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

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

配置文件

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]

如果我使用上述设置,我会得到下一个异常

If I use the above settings I get the next exception

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行

ErrorException: Runtime Notice: Declaration of Application\Sonata\UserBundle\Form\Handler\ProfileFormHandler::process() should be compatible with Sonata\UserBundle\Form\Handler\ProfileFormHandler::process(FOS\UserBundle\Model\UserInterface $user) in D:\xampp\htdocs\misplanes.dev\src\Application\Sonata\UserBundle\Form\Handler\ProfileFormHandler.php line 8

如果我更改 services.yml

And if I change the 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".

ServiceNotFoundException: The service "sonata.user.profile.form.handler" has a dependency on a non-existent service "sonata_user.profile.form".

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

I don't really know where the mistake is, I have tried a lot of configurations and I have read different forums and blogs but I have not found the solution. I will really appreciate your help. Thanks

推荐答案

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

I finally have found the solution. In the code above I had various mistakes.

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

  1. The ProfileType.php is ok, but I change the return parameter in the GetName() just to avoid conflicts, so, the code is here

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 语句中发现了一个错误,所以,正确的代码是...

  • ProfileFormHandler.php: I found a mistake here in the uses statement, so, the correct code is...

    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.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:

  • 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) }} 查看新字段,但我不知道为什么这不起作用,所以我不得不把字段:

    Finally, Another mistake was related with the template, I was using the {{ form_rest(form) }} to see the new fields, but I don't know why this didn't work, so I had to put the fields with:

    {{ form_label(form.ciudad, 'CIUDAD') }}
    {{ form_errors(form.ciudad) }}
    {{ form_widget(form.ciudad) }} 
    

    附注.对不起,我的英语水平 xD..

    PS. Sorry for my english level xD..

    这篇关于SonataUserBundle 覆盖表单配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆