HI大家我正在使用FOSUserBundle来管理项目中的用户,我想做的是 - 几乎相同的事情 - 在包含该配置文件的页面内移动/复制密码更改模板当前用户,它工作正常,它为我显示ChangePasswordFormType,但是当我向控制器提交更改时它给我一个这样的错误:
Catchable Fatal Error: Argument 1 passed to User\UserBundle\Entity\Acces::checkAllFields() must be an instance of Symfony\Component\Validator\ExecutionContext, instance of Symfony\Component\Validator\Context\ExecutionContext given
那是我的控制器:
$request = $this->get('request');
$em = $this->getDoctrine()->getManager();
$roles = $em->getRepository('AdminAdminBundle:Role')->findAll();
$userManager = $this->get('fos_user.user_manager');
$user = new \User\UserBundle\Entity\Acces();
$user = $this->container->get('security.context')->getToken()->getUser();
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->container->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->container->get('fos_user.change_password.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
$currentuser = $user;
$personnel = $em->getRepository('AdminAdminBundle:Personnel')->findBy(array('acces' => $user));
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('Cet utilisateur n\'a pas d\'acces à ce contenu.');
}
if ($request->isMethod('POST')) {
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->get('fos_user.change_password.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isValid()) {
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->container->get('fos_user.user_manager');
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->container->get('router')->generate('personnel_profile');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
}
查看代码是:
<form role="form" name="passchange" method="post" {{ form_enctype(password_form) }} action="{{ path('personnel_update_personnal') }}" id="form_sample_2">
<input type="hidden" name="passchange" value="0" />
<div class="form-group">
<label class="control-label">Mot de passe actuel</label>
{{ form_widget(password_form.current_password,{ 'attr': {'required': 'required', 'class':'form-control'} }) }}
<span class="help-block">
{{ form_errors(password_form.current_password) }} </span>
</div>
<div class="form-group">
<label class="control-label">Nouveau mot de passe</label>
{{ form_widget(password_form.plainPassword.first,{ 'attr': {'required': 'required', 'class':'form-control'} }) }}
<span class="help-block">
{{ form_errors(password_form.plainPassword.first) }} </span>
</div>
<div class="form-group">
<label class="control-label">Confirmation mot de passe</label>
{{ form_widget(password_form.plainPassword.second,{ 'attr': {'required': 'required', 'class':'form-control'} }) }}
<span class="help-block">
{{ form_errors(password_form.plainPassword.second) }} </span>
</div>
<div class="margin-top-10">
<button type="submit" class="btn green-haze">Valider le changement</button>
<button type="reset" class="btn default reset">Annuler le changement</button>
</div>
</form>
一个简单的说明:在我的案例中,实体User(属于userBundle)名为Acces
上面给出的错误是在以下行生成的:$ form-&gt; handleRequest($ request);
如果有人遇到此错误,那么任何信息都可能会有所帮助 非常感谢。
编辑:使用实体:
namespace User\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\ExecutionContext;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
* @Assert\Callback(methods={"checkAllFields"})
* @ORM\AttributeOverrides({
* @ORM\AttributeOverride(name="username", column=@ORM\Column(nullable=true)),
* @ORM\AttributeOverride(name="email", column=@ORM\Column(nullable=true)),
* @ORM\AttributeOverride(name="password", column=@ORM\Column(nullable=true)),
* })
*/
class Acces extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="\Admin\AdminBundle\Entity\Role" )
* @ORM\JoinColumn(name="role_id", referencedColumnName="id")
* @Assert\Null(groups={"Registration"})
*/
private $role;
public function __construct()
{
parent::__construct();
// your own logic
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set role
*
* @param \Admin\AdminBundle\Entity\Role $role
* @return Acces
*/
public function setRole(\Admin\AdminBundle\Entity\Role $role = null)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* @return \Admin\AdminBundle\Entity\Role
*/
public function getRole()
{
return $this->role;
}
public function checkAllFields(ExecutionContext $context) {
if (!(($this->username == null && $this->password == null && $this->email == null && $this->role == null )
|| ($this->username != null && $this->password != null && $this->email != null && $this->role != null )
)) {
$context->validate($this->getRole(), 'Acces', array("Registration"));
}
}
public function __toString() {
return parent::__toString();
}
}