Zend Framework 2 - 自定义表单验证 [英] Zend Framework 2 - Custom Form Validation

查看:26
本文介绍了Zend Framework 2 - 自定义表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要自定义表单验证.所以我写了一个类似于this一个.

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

到目前为止一切顺利.但我真的有问题把 PHP 文件/类放在哪里.

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

它应该把它放在 vendor/下作为一个看起来合乎逻辑的库.我使用了这个路径:vendor/company/Validator/MyValidator.php.但我不知道如何包括我的班级.

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?

互联网上有很多教程,但大多数都涉及验证逻辑,而不是 Zend 的基础知识".

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:

我的代码如下:

        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...

推荐答案

我现在假设一个目录结构.

I will be assuming for a directory structure now.

供应商名称:MyStuff

FQCN:MyStuff\Validator\AwesomeValidator;

文件路径:./vendor/mystuff/library/MyStuff/Validator/AwesomeValidator

鉴于 Zend Framework 2 遵循 PSR-0 标准用于自动加载,您需要将自动加载器路径添加到供应商自动加载器.ZF2 使用 Composer 来处理自动加载.当您通过 Composer 安装您的 OWN Vendor 时,这是自动完成的.如果您需要手动执行此操作,请在 composer.json

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.

这篇关于Zend Framework 2 - 自定义表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆