Answer the question
In order to leave comments, you need to log in
How to validate data in Symfony 4 form classes?
Hello! Created a form class
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstName', TextType::class, [
'constraints' => [
new NotBlank(),
new Length(['min' => 30]),
]
])
->add('lastName', TextType::class)
->add('username', TextType::class)
->add('email', EmailType::class)
->add('imageFile', FileType::class)
->add('currentPassword', PasswordType::class, [
'mapped' => false
])
->add('plainPassword', RepeatedType::class, [
'type' => PasswordType::class
])
->add('submit', SubmitType::class);
}
}
/** @var User $user */
$user = $security->getUser();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$userService->updateProfile($user);
$url = $this->generateUrl('fos_user_profile_show');
return new RedirectResponse($url);
}
FOS\UserBundle\Model\User:
properties:
plainPassword:
- Length:
min: 6
minMessage: fos_user.password.short
groups: [BugSquasherRegistration, BugSquasherResetting]
- Regex:
pattern: '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\[email protected]$!%*?&]{6,}$/'
message: Password must have at least one uppercase letter, one lowercase letter, one number and one special character
groups: [BugSquasherRegistration, BugSquasherResetting]
App\Entity\User:
properties:
firstName:
- Length:
min: 30
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question