Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should Regex constructor parameter be mandatory? #12

Closed
weierophinney opened this issue Dec 31, 2019 · 1 comment
Closed

Should Regex constructor parameter be mandatory? #12

weierophinney opened this issue Dec 31, 2019 · 1 comment
Assignees
Milestone

Comments

@weierophinney
Copy link
Member

I can obtain a Regex validator from the ValidatorPluginManager like so:

$validatorManager = $container->get('ValidatorManager');
$validator = $validatorManager->get('regex', ['pattern' => '/abcd/i']);

However, if I attempt to obtain a Regex validator without the options (to configure it later) I received a PHP Fatal error:

$validatorManager = $container->get('ValidatorManager');
$validator = $validatorManager->get('regex');
$validator->setPattern('/abcd/i');
PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function Zend\Validator\Regex::__construct(), 0 passed in <REDACTED>/vendor/zendframework/zend-servicemanager/src/Factory/InvokableFactory.php on line 30 and exactly 1 expected in <REDACTED>/vendor/zendframework/zend-validator/src/Regex.php:51
Stack trace:
#0 <REDACTED>/vendor/zendframework/zend-servicemanager/src/Factory/InvokableFactory.php(30): Zend\Validator\Regex->__construct()
#1 <REDACTED>/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(758): Zend\ServiceManager\Factory\InvokableFactory->__invoke(Object(Zend\ServiceManager\ServiceManager), 'Zend\\Validator\\...', NULL)
#2 <REDACTED>/vendor/zendframework/zend-servicemanager/src/Servic in <REDACTED>/vendor/zendframework/zend-validator/src/Regex.php on line 51

Both the approaches of passing options to the ValidatorPluginManager and configuring the validator after invocation work fine for all validators except Regex. From what I can tell, the Regex validator is the only one in the zendframework/zend-validator package where the constructor parameter is mandatory:

/**
 * Sets validator options
 *
 * @param  string|Traversable $pattern
 * @throws Exception\InvalidArgumentException On missing 'pattern' parameter
 */
public function __construct($pattern)
{
    // ...
}

For all the other validators the contructor parameters are optional, which is exactly how the AbstractValidator describes it:

/**
 * Abstract constructor for all validators
 * A validator should accept following parameters:
 *  - nothing f.e. Validator()
 *  - one or multiple scalar values f.e. Validator($first, $second, $third)
 *  - an array f.e. Validator(array($first => 'first', $second => 'second', $third => 'third'))
 *  - an instance of Traversable f.e. Validator($config_instance)
 *
 * @param array|Traversable $options
 */
public function __construct($options = null)
{
    // ...
}

Everything works fine using the first approach above, and it may be a deliberate design decision for the Regex contructor parameter to be mandatory, but it appeared out of place to me so I thought it worth mentioning in case this was an oversight.


Originally posted by @mrobinsonuk at zendframework/zend-validator#200

@gsteel
Copy link
Member

gsteel commented Jun 25, 2024

Regex constructor param is mandatory as of #280

@gsteel gsteel closed this as completed Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants