我一直在尝试从FOSUserBundle覆盖ProfileFormType。我遵循了previous问题中的步骤,该问题已经解决,因为我的当前任务与上一个任务有些相似。 (我将AngularJS用于前端)
不幸的是,我似乎无法从父类中覆盖buildForm()函数。
我记录了表格错误,上面写着:
错误:此表单不应包含其他字段。
当前密码:
错误:此值应该是用户的当前密码。
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 }
配置文件
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);
}
}
}]);
最佳答案
问题是您扩展了个人资料表单,但是在表单模板中没有主要字段存在。从您的表单类型中删除以下行:
public function getParent() {
return 'fos_user_profile';
}
或将缺少的字段
current_password
添加到edit.html
。关于javascript - 无法覆盖FOSUserBundle ProfileFormType buildForm()函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32524825/