I am using a stand alone Zend Form (I'm not using the full blown ZF2 MVC) and I have specified the following class to define the form:
use Zend\Form\Annotation;
/**
* @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
*/
class Student
{
/**
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Options({"label":"Student code"})
* @Annotations\Validator({"name":"Regex", "options":{"pattern":"/^[0-9]+$/"}})
* @Annotation\Required({"required":"true"})
*/
public $student_code;
}
This is the relevant code in my controller (simplified to only show the relevant parts)
public function createAction()
{
$request = $this->getRequest();
$student = new Student();
$builder = new AnnotationBuilder();
$form = $builder->createForm($student);
$form->bind($student);
$form->setData($request->getPost());
if ($form->isValid()) {
var_dump($form->getPost());
}
}
The problem is that when I submitt the form and with 'abc' as the value for student_code, the form is returning as valid. According to the Regex it should only accept numbers.
The required part works; the form is invalid if student_code is empty. My question is, what am I missing that the Regex is not working?
replace with:
@Annotation\Validator({"name":"Regex", "options":{"pattern":"/^[0-9]+$/"}})
Note that I have removed the s at the end from @Annotation