2012-12-18 13:08

Zend Framework 2 - 自定义表单验证

  • php
  • validation

I needed a custom form validation. So I wrote a function similar to this one.

So far so good. But I really have problems where to put the PHP file/class.

It was supposed to put it under vendor/ as a lib which seems logical. I used this path: vendor/company/Validator/MyValidator.php. But I couldn't figure out how to include my class.

Could anyone give me a short how-to for setting up the vendor module or whatever is necessary to use a custom validator?

There are a lot of tutorials on the internet but most of them deal with the logic of validation and not the "basics" for Zend.



I found this link and configured my code as follows:

My code is as follows:

        use MyStuff\Validator\CustomValidator as CustomValidator;
            'name'     => 'zip',
            'required' => false,
            'filters'  => array(
                array('name' => 'Int'),
             'validators' => array(
                      'name' => array( new CustomValidator ),
                      'options' => array(
                          'min' => 1,

When I try to submit the form I see part of the validators code as plain text and get a fatal error that the class couldn't be found...

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享