The admin will be able to edit the users of his website, but not the password. So I've created this EditUserFormType :
namespace CAPUserBundle\Form\Type;
use CAPShopAdminBundle\Repository\DiscountGridRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class EditUserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', EmailType::class);
$builder->add('discountGrid', EntityType::class, array(
'class' => 'CAPShopAdminBundle:DiscountGrid',
'choice_label' => 'name',
'placeholder' => '-- Aucune --',
'required' => false,
'query_builder' => function (DiscountGridRepository $r) {
$qb = $r->createQueryBuilder('d');
$qb->orderBy('d.name', 'ASC');
return $qb;
},
'attr' => array("autocomplete" => "off"),
));
$builder->add('isActive', ChoiceType::class, array(
'choices' => array(
'Actif' => '1',
'Désactivé' => '0'
),
));
$builder->add('name', TextType::class);
$builder->add('firstname', TextType::class);
$builder->add('company', TextType::class);
$builder->add('address', TextType::class);
$builder->add('postcode', TextType::class);
$builder->add('city', TextType::class);
$builder->add('phone', TextType::class);
$builder->add('fax', TextType::class);
$builder->add('save', SubmitType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => 'CAPUserBundle\Entity\User',
]);
}
public function getName()
{
return 'edit_user_form';
}
}
But when i validate the form, there is an error with the plainPassword field from my User entity.
This value should not be blank. edit_user
Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).data.plainPassword =
Indeed, there is a NotBlank validation constraint on it in order to force the user to set the password when he is registering.
My question is : how can I bypass this constraint for editing my User ? The password field isn't present on the EditUserFormType, so the plainPassword is useless. But I'm stuck because I use the same User Entity where is set the NotBlank value.
For the moment, I force the plainPassord field with :
$user->setPlainPassword('Ap@ssw0rd');
But this is an awful way to bypass the constraint...