doz59484 2012-12-18 13:08
浏览 54
已采纳

Zend Framework 2 - 自定义表单验证

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.

Thanks!

Edit:

I found this link and configured my code as follows:

My code is as follows:

        use MyStuff\Validator\CustomValidator as CustomValidator;
        ...
        $inputFilter->add($factory->createInput(array(
            'name'     => 'zip',
            'required' => false,
            'filters'  => array(
                array('name' => 'Int'),
            ),
             'validators' => array(
                  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...

  • 写回答

2条回答 默认 最新

  • duanpendan8067 2012-12-18 13:52
    关注

    I will be assuming for a directory structure now.

    Vendor name: MyStuff

    FQCN: MyStuff\Validator\AwesomeValidator;

    FilePath: ./vendor/mystuff/library/MyStuff/Validator/AwesomeValidator

    Given that Zend Framework 2 follows the PSR-0 Standard for Autoloading, you will need to add the autoloader path to your vendor autoloaders. ZF2 uses Composer to handle the autoloading. When you install your OWN Vendor via Composer, this is done automatically. If you need to to this manually, you do it inside composer.json

    "autoload": {
        "psr-0": {
            "Mystuff\\": "vendor/mystuff/library/"
        }
    }
    

    I hope this will give you enough to work with.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?