phpvalidationzend-framework2zfcuser

Future Proofing Changes to Validation of Username/Password Field in ZFCUser


I've been looking around at how to change the actual validation process of the registration fields in the ZFCUser module in Zend Framework 2.

There is a lot about extending and adding new fields etc. to the form but not validating these fields or extending the existing validation.

I have taken a look inside the code and found the RegistrationForm.php file and added my customer Regular Expression filters.

This works well and as expected but I am worried about this being over-written on any future upgrade.

How would I go about doing this so it is upgrade safe? Is it a case of extending a specific class or adding it as a local file in my custom modules as I have done with the view files.


Solution

  • I've same problem as you, and also do not find proper solution. But IMHO better way than change original source code is to override one of the ZfcUser services.

    There is a service called 'zfcuser_change_password_form' defined in zfc-user Module.php. If you create own service with same name - the original one will be overriden. So, first you need to define your own filter / validator class (YourFilter), then in your Module.php add:

    public function getServiceConfig()
    {
        return array(
            // ...
            'factories' => array(
                // ... 
                'zfcuser_change_password_form' => function ($sm) {
                    $options = $sm->get('zfcuser_module_options');
                    $form = new \ZfcUser\Form\ChangePassword(
                           null, $sm->get('zfcuser_module_options')
                        );
                    $form->setInputFilter(
                           new \YourModule\Form\YourFilter($options)
                        );
                    return $form;
                },
            ),
        );
    }
    

    Such solution allows to update zfcuser without overriding your changes.