1

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 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

this helped a lot! now the final thing: how to use the validator in the model (ie Entity.php). I sticked to the tutorial creating the InputFilter... I'll update my post!
Without seeing your code for your custom decorator i can just suggest to do $cv= new CustomValidator(array('min'=>1)); and then 'validators'=>array($cv)
my fault... just a stupid mistake in the end. thx for your help!
every time i use composer my manual include is beeing deleted. could you help me out and show me how i can install it "permanently"? thx!
This is a composer-configuration thingy. I suggest opening up another question with both tags, zend-framework2 and composer so you get better help!
0

An alternative to using composer is a more manual approach which might avoid your include issue. The comments on this page may be helpful to show you where to put things manually: http://framework.zend.com/manual/2.0/en/modules/zend.validator.writing-validators.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.