I have one thing I'm not sure how to solve in a good way: I'm trying to use ZF2 InputFilterManager for fetching my input-filters, which make use of some own validators.
I added the validators to service manager config, so they can be fetched by InputFilterManager and get injected by keyword reference inside the InputFilter definition. that works fine, as far as validators doesn't need anyting to get injected or some objects, that are available from the ServiceLocator.
But one validator needs to get an array injected to do an in_array check inside. It is not possible to inject that array inside the validator factory, because it depends on business logic that runs before InputFilter isValid() call happens.
Without using service manager I would inject that array with constructor of the validator and would initialize it after business logic was called. So the question is: If I remove injection by constructor to use InputFilterManager I would need to inject it later by using: $inputFilter->get('element')->getValidatorChain()->plugin(Validator\ArrayCheck::class)->setPossibleValues($array) - is this the way to go?
I quite like to have necessary dependencies getting injected into classes by using the constructor and it feels somehow dirty to rewrite validator to rely on a setter injection and add a check to verify the setter was used before running isValid() logic.
Some code to explain my concern:
return [
'validators' => [
'invokables' => [
Validator\ArrayCheck::class => Validator\ArrayCheck::class
]
],
'input_filters' => [
'invokables' => [
InputFilter\Foo::class => InputFilter\Foo::class,
],
],
];
final class Process extends InputFilter
{
public function init()
{
$this->add(
[
'name' => 'foo',
'required' => true,
'allow_empty' => false,
'validators' => [
['name' => Validator\ArrayCheck::class]
],
]
);
}
}
class ArrayCheck extends AbstractValidator
{
/**
* @param array $possibleValues
* @param array|Traversable $options
*/
public function __construct(array $possibleValues = [], $options = null)
{
parent::__construct($options);
$this->possibleValues = $possibleValues;
}
/**
* @param array $possibleValues
*/
public function setPossibleValues(array $possibleValues)
{
$this->possibleValues = $possibleValues;
}
...
}
Any opinions on that?
I think you should have a look at the
Callbackfilter andCallbackvalidator classes.A callback filter runs a function or a class method when filtering your value. You can read on the
Callbackfilter class here in the ZF2 documentation. You can find the class here on GitHUB.A callback validator runs a function or a class method when validating your value. You can read on the
Callbackvalidator class here in the ZF2 documentation. You can find the class here on GitHUB.They both use the PHP function
call_user_func_arrayinternally.So let's say you need to filter using your
dependent business logicin a classMy\Filter\Dependencyand your method is calledfilterLogicand/or you need to validate using yourdependent business logicin a classMy\Validator\Dependencyand your method is calledvalidateLogic. You do that like this:Both the
Callbackclasses will callcall_user_func_arraywith your values forcallbackandoptionsas arguments.If you need the values from your input-filter in the callback functions then you will have to get them from your
InputFilterinstance manually ($inputFilter->getRawValues();) and pass them to the function as an argument.If this is not solving your issue please try to explain better what you need. I can try to come up with something else :D