我需要自己的表单来添加/编辑用户。在我的系统中,我有employee和fos_user。有些员工可以访问系统。
我为员工和fos_user制作了大表格。当我补充用户名时,创建的电子邮件和密码(repeat_password)是fos_user。但是当我编辑fos_user时,用户名和电子邮件都有基础值,但密码为空。点击“保存”后,表单提交,我看到错误:
An exception occurred while executing 'UPDATE fos_user SET password = ? WHERE id = ?' with params [null, 2]:
我知道。 Doctrine查看更改密码,并更新新值(null)。
我如何禁用映射密码?
编码我的课程表格:
$builder->addEventListener(FormEvents::POST_BIND, function(FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
$username = $form->get('username')->getData();
$password = $form->get('password')->getData();
$repeatPassword = $form->get('repeat_password')->getData();
$enabled = $form->get('enabled')->getData();
// Sprawdzamy czy ktoś chce stworzyć konto - wpisał cokolwiek w sekcji "Dostep".
if(!empty($username) || !empty($enabled)){
if(empty($username)){
$form->get('username')->addError(new FormError('Brak nazwy użytkownika'));
}
if(!empty($password) || !empty($repeatPassword)){
if(empty($password)){
$form->get('password')->addError(new FormError('Brak hasła'));
}
if(empty($repeatPassword)){
$form->get('repeat_password')->addError(new FormError('Brak hasła'));
}
if($password != $repeatPassword){
$error = 'Hasło się różnią!';
$form->get('password')->addError(new FormError($error));
$form->get('repeat_password')->addError(new FormError($error));
}
// Zmienia text na plainPassword - zaszyfrowane hasło
$data->setPlainPassword($password);
}
}
});
答案 0 :(得分:0)
以下是我在没有用户密码的情况下编辑用户的技巧。我希望它可以在你的实例中起作用。此示例是在没有FOSUserBundle的环境中完成的,但可以扩展。
现在可以在没有密码的情况下编辑用户了!
class UserEditType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('enabled', 'choice', array(
'choices' => array('Yes' => 'Yes', 'No' => 'No'),
'expanded' => true,
'multiple' => false,
'label' => 'Enabled: ',
))
->add('fname')
->add('sname')
->add('email')
->add('username')
->add('role', 'choice', array(
'choices' => array('ROLE_USER' => 'User', 'ROLE_ADMIN' => 'Admin'),
'expanded' => true,
'multiple' => false,
'label' => 'Group: ',
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'Mana\ClientBundle\Entity\User',
'validation_groups' => array('edit'),
));
}
* @ORM\Column(name="userpass", type="string", length=100, nullable=false)
* @Assert\NotBlank(message="Password may not be empty")
* @Assert\Length(
* min = "5",
* max = "12",
* minMessage = "Password must be at least 5 characters long",
* maxMessage = "Password cannot be longer than than 12 characters",
* groups = {"Default"}
* )
public function updateAction(Request $request, $id) {
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('ManaClientBundle:User')->find($id);
$form = $this->createForm(new UserEditType(), $user);
$form->bind($request);
if ($form->isValid()) {
$em->persist($user);
$em->flush();
return $this->redirect($this->generateUrl('user_main', array()));
}
return array(
'form' => $form->createView(),
'user' => $user,
'title' => 'Edit user',
);
}
答案 1 :(得分:0)
我走了另一条路。我不知道,也许我的解决方案是错误的。
bulidForm:
...
->add('password', 'password', array(
'label' => 'Password',
'required' => false,
'mapped' => false
))
->add('repeat_password', 'password', array(
'label' => 'Repeat Password',
'required' => false,
'mapped' => false
))
...
$builder->addEventListener(FormEvents::POST_BIND, function(FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
...
if(!empty($username) || !empty($enabled)){
// check conditions
$data->setPassword($password);
$data->setPlainPassword($password);
}
}
如果我不使用setPassword()和setPlainPassword(),则不会为我生成密码。