mhc
mhc

Reputation: 31

Symfony 4 - integer validation

i have a problem with validation in Symfony 4.

I have code like this:

This is my Entity Class:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
 * @ORM\Entity(repositoryClass="App\Repository\AdminsRepository")
 * @UniqueEntity("email")
 * @UniqueEntity("login")
 */
class Admins
{
    .....
    /**
     * @ORM\Column(type="boolean")
     * @Assert\Type(
     *     type="integer",
     *     message="The value {{ value }} is not a valid {{ type }}."
     * )
     */
    private $type;

    public function getId(): ?int
    {
        return $this->id;
    }
    ...
    public function getType(): ?bool
    {
        return $this->type;
    }

    public function setType(bool $type): self
    {
        $this->type = $type;

        return $this;
    }
}

This is my Controller class:

...

class AdminController extends AbstractController {
.......
    /**
     * @Route("/admin/add", name="admin_add")
     */
    public function add(Request $request) {
        $admins_object = new Admins();
        $form = $this->createFormBuilder($admins_object)
                ->add('first_name', TextType::class, ['label' => 'Imię'])
                ->add('last_name', TextType::class, ['label' => 'Nazwisko'])
                ->add('login', TextType::class, ['label' => 'Login'])
                ->add('email', EmailType::class, ['label' => 'E-mail'])
                ->add('password', TextType::class, ['label' => 'Hasło'])
                ->add('type', IntegerType::class, ['label' => 'Typ'])
                ->add('save', SubmitType::class, ['label' => 'Zapisz'])
                ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($admins_object);
            $entityManager->flush();

            $this->addFlash(
                    'notice', 'Dane zostały poprawnie zapisane!'
            );
            return $this->redirect($request->getUri());
        }


        return $this->render('admin/add.html.twig', [
                    'form' => $form->createView()
        ]);
    }

.......

}

and my view:

{{ form(form, {'attr': {'novalidate': 'novalidate'}}) }}

When I put some integer in 'type' field (for example '1') - validator shows me message like "Type field isn't valid integer...".

Can you help me? Why integer validation doesn't work?

Upvotes: 1

Views: 2172

Answers (1)

Fabien Papet
Fabien Papet

Reputation: 2319

This is expected behaviour, because the TypeValidator executed by the Type executes is_type on the value.

So types int and integer will be executed with is_int and return false as you can see in the docs.

See : https://secure.php.net/manual/en/function.is-int.php

A working workaround will use IntegerType instead of TextType.

Upvotes: 2

Related Questions