0
votes

I'm building a form in ZF2 from an entity and everything seems to work fine, except 2 of my validators are ignored for some reason. The Entity looks like this:

  /**
   * @var string $name
   *
   * @ORM\Column(name="name", type="string", length=255, nullable=true)
   * @Annotation\Attributes({"type":"text"})
   * @Annotation\Validator({"name":"NotEmpty"}) // duplicate
   * @Annotation\Options({"label":"Name:"})

   */
  private $name;

  /**
   * @var integer $sort
   *
   * @ORM\Column(name="sort", type="integer")
   * @Annotation\Attributes({"type":"text"})
   * @Annotation\Validator({"name":"Int"}) 
   * @Annotation\Validator({"name":"NotEmpty"})
   * @Annotation\Options({"label":"Sort:"})

   */
  private $sort;  

Yet I can submit the form without any values. I can enter a string in the SORT input, I can leave both fields empty. Why is this not working, why is there no errormessage when I leave the fields empty?

1

1 Answers

2
votes

okay, it's partially because I'm STUPID.

Problem 1: notempty not working is because I defined nullable as true. Seems to override the "notempty" validator. duh.

Problem 2: I still don't know WHY that works, but if I declare the column type as string (instead of integer), suddenly the int validation works.

So here's the right code:

  /**
   * @var string $name
   *
   * @ORM\Column(name="name", type="string", length=255)
   * @Annotation\Attributes({"type":"text"})
   * @Annotation\Validator({"name":"NotEmpty"}) 
   * @Annotation\Options({"label":"Name:"})

   */
  private $name;

  /** 
   * @var integer $sort
   *
   * @ORM\Column(name="sort", type="string")
   * @Annotation\Attributes({"type":"text"})
   * @Annotation\Validator({"name":"Int"}) 
   * @Annotation\Validator({"name":"NotEmpty"})
   * @Annotation\Options({"label":"Sort:"})

   */
  private $sort;