Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 Custom Validators for Forms

I'm trying to make a user registration form which checks for the complexity of the password field. I've written a custom validator to do this according to the documentation. This file lives in my 'User' module at User\src\User\Validator.

<?php

namespace User\Validator;

use Zend\Validator\AbstractValidator;

class PasswordStrength extends AbstractValidator {

const LENGTH = 'length';
const UPPER  = 'upper';
const LOWER  = 'lower';
const DIGIT  = 'digit';

protected $messageTemplates = array(
    self::LENGTH => "'%value%' must be at least 6 characters long",
    self::UPPER => "'%value% must contain at least one uppercase letter",
    self::LOWER => "'%value% must contain at least one lowercase letter",
    self::DIGIT => "'%value% must contain at least one digit letter"
);

public function isValid($value) {
    ... validation code ...
}
}

My problem arises in trying to use this validator in my user registration form. I tried adding the validator to the ServiceManager by configuring it in Module.php.

public function getServiceConfig() {
    return array(
        'invokables' => array(
            'PasswordStrengthValidator' => 'User\Validator\PasswordStrength'
        ),
    );
}

Then I added it to the input filter in User.php.

public function getInputFilter() {
    if (!$this->inputFilter) {
        $inputFilter = new InputFilter();
        $factory     = new InputFactory();

        $inputFilter->add($factory->createInput(array(
            'name'     => 'username',
            'required' => true,
            'validators' => array(
                array(
                    'name'    => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min'      => 1,
                        'max'      => 100,
                    ),
                ),
            ),
        )));

        $inputFilter->add($factory->createInput(array(
            'name'     => 'password',
            'required' => true,
            'validators' => array(
                array(
                    'name'    => 'PasswordStrengthValidator',
                ),
            ),
        )));

        $this->inputFilter = $inputFilter;
    }

    return $this->inputFilter;
}

However, when I access the form and hit the submit button, I get a ServiceNotFoundException.

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for PasswordStrengthValidator

Is there a problem with my ServiceManager configuration? I'm not even sure if this is the appropriate way to use a custom validator in the first place. I've found plenty of examples using ZF1, but the documentation and examples for ZF2 that I've found never extend beyond the writing of the validator to address its integration with forms, etc. Any advice would be greatly appreciated.

like image 641
The Doge Prince Avatar asked Nov 20 '12 15:11

The Doge Prince


1 Answers

you can try this workaround... registrer your validator in Module.php but with function getValidatorConfig or in module.config.php under key 'validators'.

public function getValidatorConfig() {
  return array(
    'invokables' => array(
        'PasswordStrengthValidator' => 'User\Validator\PasswordStrength'
    ),
  );
}

Then in your User.php, try this: (but you must have access to service locator, you can inject it from UserFactory etc.)

$validatorManager = $this->getServiceLocator()->get('ValidatorManager');
// here you can test $validatorManager->get('PasswordStrengthValidator');

$validatorChain = new ValidatorChain();
$validatorChain->setPluginManager($validatorManager);

$inputFilter = new InputFilter();   
$inputFilter->getFactory()->setDefaultValidatorChain($validatorChain);

This works for me.

Martin

like image 91
Martin Vlach Avatar answered Sep 22 '22 19:09

Martin Vlach