1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answer. I changed: ->add('type', TextType::class, ['label' => 'Typ']) to: ->add('type', IntegerType::class, ['label' => 'Typ']) and now, when I put integer, i get validation error, and when I put string, there is no validation error :) Have you any idea?
You may be wrong somewhere, I 've just tested with what you provide and it is working. Can you edit your question so I can see what your problem is ?
Why are you checking for integer as your getType returns bool ?
You were right, i changed field type in database to integer and everything is all right. Thanks. How is good form field for boolean type?
I'd say checkbox type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.