无法覆盖 FOSUserBundle ProfileFormType buildForm() 函数

Can't override FOSUserBundle ProfileFormType buildForm() function

我一直在尝试覆盖 FOSUserBundle 中的 ProfileFormType。我按照 问题中的步骤进行操作,该问题已解决,因为我当前的任务与上一个任务有些相似。 (我使用 AngularJS 作为前端)

不幸的是,我似乎无法覆盖父级的 buildForm() 函数 class。

我已经记录了表单错误,上面写着:

ERROR: This form should not contain extra fields.

current_password:

ERROR: This value should be the user's current password.

services.yml

services:
    acme.profile.form.type:
        class: Acme\BulletinBundle\Form\Type\ProfileFormType
        arguments: ["%fos_user.model.user.class%"]
        tags:
            - { name: form.type, alias: acme_user_profile }

config.yml

fos_user:
    profile:
        form:
            type: acme_user_profile

ProfileFormType.php

namespace Acme\BulletinBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ProfileFormType extends AbstractType {

    private $class;

    public function __construct($class) {
        $this->class = $class;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
            ->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
        'data_class' => $this->class,
        'intention' => 'profile',
        'csrf_protection' => false,
        ));
    }

    public function getParent() {
        return 'fos_user_profile';
    }

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

UserController.php

/**
 * @Method("POST")
 * @Route("/user/edit", name="user_edit", options={"expose"=true})
 */
public function editAction(Request $request) {
    $id = $request->request->get('id');
    $user = $this->getDoctrine()
            ->getManager()
            ->getRepository("AcmeBulletinBundle:User")
            ->find($id);

    /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
    $formFactory = $this->get('fos_user.profile.form.factory');

    $form = $formFactory->createForm();
    $form->setData($user);

    $form->handleRequest($request);

    if ($form->isValid()) {
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');
        $userManager->updateUser($user);
        return new JsonResponse(['valid' => true]);
    }
    $errs = (string) $form->getErrors(true, false);
    return new JsonResponse(['valid' => $errs]);
}

edit.html

<form class="form-group text-left" ng-submit="submit()" novalidate name="userFrm">
    <div class="form-group">
        <label for="user.email" class="required">Email</label>
        <input id="user.email" name="user.username" class="form-control" required type="text" ng-model="user.email" />
    </div>
    <div class="form-group">
        <label for="user.username" class="required">Username</label>
        <input id="user.username" name="user.username" class="form-control" required type="text" ng-model="user.username" />
    </div>

    <input type="submit" value="Update" ng-disabled="userFrm.$invalid"  class="btn btn-primary center-block col-lg-2" />
</form>

edit.js

angular.module('myApp', [])
       .controller('EditUserCtrl', ["$scope", "$http", "$state", "$stateParams", function ($scope, $http, $state, $stateParams) {

    //returns {username: data1, email: data2, enabled: data3}
    $http.get(Routing.generate('get_user', {id: $stateParams.id}))
                        .then(function (response) {
                            var user = response.data.user;
                            $scope.user = user;
                        });

    $scope.submit = function () {
                    var formData = {
                        fos_user_profile_form: $scope.user,
                        id : $stateParams.id
                    };

                    var success = function (response) {
                        var valid = response.data.valid;
                        console.log(valid);
                    };
                    var error = function (reason) {
                        alert('error');
                    };

                    console.log($scope.user);

                    $http.post(Routing.generate('user_edit'), $.param(formData), {
                        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                    })
                            .then(success, error);
                }
    }
}]);

问题是您扩展了配置文件表单,但在您的表单模板中 none 存在主要字段。从您的表单类型中删除此行:

public function getParent() {
    return 'fos_user_profile';
}

或将缺少的字段 current_password 添加到 edit.html