Bonjour,
Voila je rencontre un petit problème avec mon code.
J'ai fais un système utilisateur à l'aide de symfony 4.
Je n'utilise pas FOSUserBundle, mais l'ai fait a la main avec la doc.
Sur la page édition de mon utilisateur, je voudrais que si je laisse le champs mot de passe vide, cela me laisse le mot de passe actuelle et ne fasse que la modification sur les autres champs.
Voici mon code de mon controller:
public function edit(Request $request, User $user, UserPasswordEncoderInterface $passwordEncoder): Response
{
$form = $this->createForm(EditUserFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
return $this->redirectToRoute('index_user_back');
}
return $this->render('back/users/edit.html.twig', [
'user' => $user,
'registrationForm' => $form->createView()
]);
}
Voici le code de mon form:
<?php
namespace App\Form\Back;
use App\Entity\User;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Form\CallbackTransformer;
class EditUserFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email')
->add('name')
->add('firstname')
->add('plainPassword', PasswordType::class, [
// instead of being set onto the object directly,
// this is read and encoded in the controller
'mapped' => false,
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
]),
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],
])
->add('roles', ChoiceType::class, [
'choices' => [
'Utilisateur' => 'ROLE_USER',
'Administrateur' => 'ROLE_ADMIN',
],
'label' => 'Role :'
])
;
//roles field data transformer
$builder->get('roles')
->addModelTransformer(new CallbackTransformer(
function ($rolesArray) {
// transform the array to a string
return count($rolesArray)? $rolesArray[0]: null;
},
function ($rolesString) {
// transform the string back to an array
return [$rolesString];
}
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
Merci d'avance.
autre solution:
diviser les 2 fonctions, creer un formulaire de modification et un autre de reset / changement de password.
ici un exemple avec l'utilisation du '->remove' qui permet de ne pas afficher un champ de formulaire, il suffit de refaire un seconf formulaire en affichant uniquement la demande de changement de mot de passe et le tour est joué
<?php
namespace App\Form\Back;
use App\Entity\User;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Form\CallbackTransformer;
class EditUserFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email')
->add('name')
->add('firstname')
->remove('plainPassword')
->add('roles', ChoiceType::class, [
'choices' => [
'Utilisateur' => 'ROLE_USER',
'Administrateur' => 'ROLE_ADMIN',
],
'label' => 'Role :'
])
;
//roles field data transformer
$builder->get('roles')
->addModelTransformer(new CallbackTransformer(
function ($rolesArray) {
// transform the array to a string
return count($rolesArray)? $rolesArray[0]: null;
},
function ($rolesString) {
// transform the string back to an array
return [$rolesString];
}
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}