如何覆盖 ProfileFormType 来编辑用户? symfony2

how to override ProfileFormType to edit users? symfony2

我正在 symfony2.I 使用 FOSUserBundle,所以我想覆盖编辑用户的 FormType 以将名称添加到构建器。那我应该怎么做呢?

我做了什么:

配置文件格式类型:

<?php

namespace UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;

class ProfileFormType extends BaseType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        parent::buildForm($builder, $options);
        // add your custom field
        $builder->add('name')
                ->add('roles', 'collection', array(
                    'type' => 'choice',
                    'options' => array(
                        'choices' => array(
                            'ROLE_ADMIN' => 'Admin'))))
                ->add('image', new ImageType())

        ;
    }

    public function getName() {
        return 'user_edit_profile';
    }

}

services.yml

user.edit.form.type:
    class: UserBundle\Form\Type\ProfileFormType
    arguments: [%fos_user.model.user.class%]
    tags:
        - { name: form.type, alias: user_edit_profile }

edit.html.twig

{% extends "UserBundle::layout.html.twig" %}


{% block body %}
    <center> <h1> Modification de profile </h1> </center>
    <aside class="col-sm-3">
        <div class="panel panel-default">
            <div class="panel-heading">Modification</div>
            <div class="panel-body">
                Veuillez remplir les champs 
            </div>
        </div>
    </aside>

    <!--timeline-->
    <section class="timeline col-sm-9">
        <!--post Timeline-->
        <div class="thumbnail thumbnail-post">
            <!--caption-->
            <div class="caption">


                <form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="form-horizontal">


                    <div class="form-group">

                        {{ form_errors(form.name) }}
                        <div class="col-sm-9">
                            Name   {{ form_widget(form.name, { 'attr': {'class': 'form-control', 'placeholder': 'form.name'|trans } }) }}
                        </div>

                    </div>



                    <div class="form-group">

                        {{ form_errors(form.email) }}
                        <div class="col-sm-9">
                            Email   {{ form_widget(form.email, { 'attr': {'class': 'form-control', 'placeholder': 'form.email'|trans } }) }}
                        </div>

                    </div>
                    <div class="form-group">

                        {{ form_errors(form.username) }}
                        <div class="col-sm-9">
                            Pseudo   {{ form_widget(form.username, { 'attr': {'class': 'form-control', 'placeholder': 'form.username'|trans } }) }}

                        </div>

                    </div>

                    <div class="form-group">

                        {{ form_errors(form.current_password) }}
                        <div class="col-sm-9">
                            Mot de passe actuelle   {{ form_widget(form.current_password, { 'attr': {'class': 'form-control', 'placeholder': 'form.current_password'|trans } }) }}

                        </div>

                    </div>


                    </br>



                    {{ form_rest(form) }}


                    <div class="form-group">
                        <div class="col-md-4 col-sm-4 col-xs-12 col-md-offset-3">
                            <input class="btn btn-default submit" type="submit" value="{{ 'registration.submit'|trans }}">
                        </div>
                    </div>
                </form>
            </div> <!--#caption-->


            <!--#post timeline-->
        </div>
        <!--#timeline-->
    </section>





{% endblock %}

{% block js %}

    <script>
        $(document).ready(function () {

            $('#roles').hide();
        });


    </script>


{% endblock %} 

我遇到了这个错误:

Neither the property "name" nor one of the methods "name()", "getname()"/"isname()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" in FOSUserBundle:Profile:edit.html.twig at line 28.

我该如何解决。

更新您的服务文件:

user.edit.form.type:
    class: UserBundle\Form\Type\ProfileFormType
    arguments: [%fos_user.model.user.class%]
    decorates: fos_user.profile.form.type
    tags:
        - { name: form.type, alias: user_edit_profile }

fos_user.profile.form.type 是指我们的服务名称,它将替换名称为 user.edit.form.type

的服务

添加use Symfony\Component\Form\FormBuilderInterface;

你遇到错误:

Neither the property "name" nor one of the methods "name()", "getname()"/"isname()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" in FOSUserBundle:Profile:edit.html.twig at line 28.

由于 fos_user_user table 没有名称字段。如果你想添加不在 table 中的字段,你应该添加字段,实体中的那些字段生成 getter 和 setter,最后创建新的迁移并向上迁移。

profile:
        form:
            type: fos_user_profile

它需要在 fos

的 config.yml