Note that there are some explanatory texts on larger screens.

plurals
  1. POZend Framework 2 Custom Validators for Forms
    text
    copied!<p>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 <a href="http://framework.zend.com/manual/2.0/en/modules/zend.validator.writing-validators.html" rel="noreferrer">documentation</a>. This file lives in my 'User' module at User\src\User\Validator.</p> <pre><code>&lt;?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 =&gt; "'%value%' must be at least 6 characters long", self::UPPER =&gt; "'%value% must contain at least one uppercase letter", self::LOWER =&gt; "'%value% must contain at least one lowercase letter", self::DIGIT =&gt; "'%value% must contain at least one digit letter" ); public function isValid($value) { ... validation code ... } } </code></pre> <p>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.</p> <pre><code>public function getServiceConfig() { return array( 'invokables' =&gt; array( 'PasswordStrengthValidator' =&gt; 'User\Validator\PasswordStrength' ), ); } </code></pre> <p>Then I added it to the input filter in User.php.</p> <pre><code>public function getInputFilter() { if (!$this-&gt;inputFilter) { $inputFilter = new InputFilter(); $factory = new InputFactory(); $inputFilter-&gt;add($factory-&gt;createInput(array( 'name' =&gt; 'username', 'required' =&gt; true, 'validators' =&gt; array( array( 'name' =&gt; 'StringLength', 'options' =&gt; array( 'encoding' =&gt; 'UTF-8', 'min' =&gt; 1, 'max' =&gt; 100, ), ), ), ))); $inputFilter-&gt;add($factory-&gt;createInput(array( 'name' =&gt; 'password', 'required' =&gt; true, 'validators' =&gt; array( array( 'name' =&gt; 'PasswordStrengthValidator', ), ), ))); $this-&gt;inputFilter = $inputFilter; } return $this-&gt;inputFilter; } </code></pre> <p>However, when I access the form and hit the submit button, I get a ServiceNotFoundException.</p> <pre><code>Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for PasswordStrengthValidator </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload