Bonjour,
Voila je rencontre un petit problème avec mon code.
Je souhaiterais pouvoir modifier un utilisateur via mon backoffice mais sans être obligé de modifier le mot de passe
Voici mon userType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class, [
'label' => 'Email',
'required' => true,
'attr' => ['class' => 'form-control']
])
->add('username', TextType::class, [
'label' => 'Utilisateur',
'required' => true,
'attr' => ['class' => 'form-control']
])
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array(
'label' => 'Mot de passe',
'required' => true,
'attr' => [
'class' => 'form-control',
'minlength' => 12
]
),
'second_options' => array(
'label' => 'Repeter mot de passe',
'required' => true,
'attr' => [
'class' => 'form-control',
'minlength' => 12
]
),
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => User::class,
));
}
Voici mon AdminUserController
class AdminUserController extends AbstractController
{
/**
* @var UserRepository
*/
private $repository;
/**
* @var ObjectManager
*/
private $em;
public function __construct(UserRepository $repository, ObjectManager $em)
{
$this->repository = $repository;
$this->em = $em;
}
/**
* @Route("/admin/user-list", name="admin.users.list")
*/
public function index()
{
$users = $this->repository->findAll();
return $this->render('admin/user/index.html.twig', ['users' => $users]);
}
/**
* @Route("/admin/user-edit/{id}", name="admin.users.edit", methods="GET|POST")
* @param User $user
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function edit(User $user, Request $request)
{
$userName = $user->getUsername();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->flush();
$this->addFlash('success', 'Utilisateur modifié avec succés');
return $this->redirectToRoute('admin.users.list');
}
return $this->render('admin/user/edit.html.twig', [
'username' => $userName,
'form' => $form->createView()
]);
}
}
Et enfin mon template :
{{ form_start(form) }}
<div class="row">
<div class="col-md-3">
<div class="form-group">
{{ form_row(form.email) }}
</div>
</div>
<div class="col-md-3">
<div class="form-group">
{{ form_row(form.username) }}
</div>
</div>
</div>
{{ form_widget(form) }}
<button class="btn btn-primary">{{ button|default('Editer') }}</button>
{{ form_end(form) }}
Bonsoir.
Tu pourrais par exemple faire :
$form = $this->createForm(UserType::class, $user);
$form->remove('plainPassword');
// etc ...
Merci j'ai déjà testé cela mais il me demande que le champs ne soit pas vide. Etonnant.
Salut,
De mon côté j'aurais fait un formType d'enregistrement et un de config.
Par exemple :
RegisterUserFormType
$builder
->add('email', EmailType::class, [
'label' => 'Email',
'required' => true,
'attr' => ['class' => 'form-control']
])
->add('username', TextType::class, [
'label' => 'Utilisateur',
'required' => true,
'attr' => ['class' => 'form-control']
])
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array(
'label' => 'Mot de passe',
'required' => true,
'attr' => [
'class' => 'form-control',
'minlength' => 12
]
),
'second_options' => array(
'label' => 'Repeter mot de passe',
'required' => true,
'attr' => [
'class' => 'form-control',
'minlength' => 12
]
),
))
;
ConfigUserFormType
$builder
->add('email', EmailType::class, [
'label' => 'Email',
'required' => true,
'attr' => ['class' => 'form-control']
])
->add('username', TextType::class, [
'label' => 'Utilisateur',
'required' => true,
'attr' => ['class' => 'form-control']
])
//Puis tes config si j'amais tu as des champs dans l'avenir à ajouter pour la config de ton user
->add()
;
Pour le champs obligatoire regarde bien ta config 'required' => true, tu es donc contraint de le remplir.
J'espère que j'ai répondu à ta question.
/**
@return \Symfony\Component\HttpFoundation\Response
*/
public function edit(User $user, Request $request)
{
$oldPassword = $user->getPassword();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
$form->remove('plainPassword');
if ($form->isSubmitted() && $form->isValid()) {
// On rajoute l'ancien mot de passe
$user->setPassword($oldPassword);
$this->em->flush();
$this->addFlash('success', 'Utilisateur modifié avec succés');
return $this->redirectToRoute('admin.users.list');
}
return $this->render('admin/user/edit.html.twig', [
'username' => $userName,
'form' => $form->createView()
]);
}