A
A
Alexey Verkhovtsev2019-03-30 20:54:54
symfony
Alexey Verkhovtsev, 2019-03-30 20:54:54

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);
    }
}

controller code
/** @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);
        }

even tried to create validation in yml file
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

But nothing is validating, what's my mistake?
PS I don't use annotations

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question