0

我使用的是独立的 Zend 表单(我没有使用完整的 ZF2 MVC),并且我指定了以下类来定义表单:

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;
}

这是我的控制器中的相关代码(简化为仅显示相关部分)

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());
    }
}

问题是,当我提交表单并将“abc”作为 student_code 的值时,表单返回为有效。根据正则表达式,它应该只接受数字。

所需部分有效;如果 student_code 为空,则表格无效。我的问题是,我错过了什么正则表达式不起作用?

4

1 回答 1

0

用。。。来代替:

 @Annotation\Validator({"name":"Regex", "options":{"pattern":"/^[0-9]+$/"}})

请注意,我已从 @Annotation 中删除了末尾的 s

于 2014-07-17T12:57:03.990 回答