OmarAguinaga
OmarAguinaga

Reputation: 727

@Assert\NotBlank Not working

I have a formular that I want to validate. The user should not be able to leave fields unfilled and that why I use @Assert\NotBlank but it does not seem to be working this is a part of my entity:

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
...
     /**
     * @var string
     *
     * @ORM\Column(name="device", type="string", length=255, nullable=false)
     * @Assert\NotBlank(message="This value cannot be empty!")
     */
    private $device;
...

And in the controler I'm using formbuilder from symfony like this:

...
     $form = $this->createFormBuilder()
        ->add('device', 'text', array(
            'label' => 'Device:',
            'attr' => array('placeholder' =>'Dell 2407WPB - Monitor'),
            'required' => true,
            ))
...

Do you have any suggestions about what may I be doing wrong? I've been stuck in this problem too long.

Thanks in advance :)

Upvotes: 0

Views: 4466

Answers (1)

Federkun
Federkun

Reputation: 36934

When you create the form with createFormBuilder, you should pass an instance of the entity.

$form = $this->createFormBuilder(new MyEntity())

so that the form know the class that holds the data (and his constraints).

Upvotes: 1

Related Questions