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

Hotfix/8 nested input filter config #24

Closed
wants to merge 2 commits into from
Closed

Hotfix/8 nested input filter config #24

wants to merge 2 commits into from

Conversation

alex-patterson-webdev
Copy link

Q A
Documentation no
Bugfix yes
BC Break maybe
New Feature no
RFC no

Description

This pull request aids to address the problem defined in issue #8

I have added the ability to nest the input specification of nested input filters, when calling Factory::createInputFilter()

Currently, when nesting input filter configuration, the type key sits at the same level as the names of the required inputs. This can cause issue if the input you wish to add also has the name type,

$spec = [
    'baz' => [
        'type' => InputFilter::class,
        'foo' => [
            'name' => 'foo',
            'required' => false,
            'validators' => [
                [
                   'name' => Validator\NotEmpty::class,
                ],
            ],
        ],
    ],
];

$factory->createInputFilter($spec);

This PR allows the optional inputs key that nests the relevant inputs. If we have an input with the name type if will no longer conflict.

$spec = [
    'baz' => [
        'type' => InputFilter::class,
        'inputs' => [
            'type' => [
                'name' => 'foo',
                'required' => false,
                'validators' => [
                    [
                        'name' => Validator\NotEmpty::class,
                    ],
                ],
            ],
        ],
    ],
];

$factory->createInputFilter($spec);

BC Consideration

There is a possibility that existing code has an input already named inputs so we could consider changing this to something more unique

…e used if its name conflicted with one of the configured inputs. This fix has added a conditional check for an optional nested 'input' specification array allowing the inputs config to be nested.

Signed-off-by: Alex Patterson <[email protected]>
…romGivenConfiguration to include new optional 'inputs' nested configuration option

Signed-off-by: Alex Patterson <[email protected]>
@froschdesign
Copy link
Member

@alex-patterson-webdev

This can cause issue if the input you wish to add also has the name type

Maybe I have missed something, but you do not need to specify a key:

$spec = [
    'group' => [
        'type' => Laminas\InputFilter\InputFilter::class,
        [
            'name'       => 'type',
            'required'   => false,
            'validators' => [
                [
                    'name' => Laminas\Validator\NotEmpty::class,
                ],
            ],
        ],
    ],
];

$inputFilter = (new Laminas\InputFilter\Factory())->createInputFilter($spec);

var_dump($inputFilter->get('group')->get('type')->getName()); // 'type'

In this case the input filter uses the value of the name property of the input:

if ($input instanceof InputInterface && (empty($name) || is_int($name))) {
$name = $input->getName();
}

@froschdesign
Copy link
Member

I personally find the naming in this example very confusing:

$spec = [
    'baz' => [
        'type' => InputFilter::class,
        'inputs' => [
            'type' => [
                'name' => 'foo',
                'required' => false,
                'validators' => [
                    [
                        'name' => Validator\NotEmpty::class,
                    ],
                ],
            ],
        ],
    ],
];

Because this means:

var_dump($inputFilter->get('baz')->get('type')->getName()); // foo

@alex-patterson-webdev
Copy link
Author

@froschdesign Thanks, I came across this issue today and google took me to issue #8. I had just assumed it wasn't possible.

It seems you are correct that the inputs can be specified with numeric keys; providing they have a name. The section of code is in fact near where I added my changes so I perhaps should have noticed. Anyway, this is great and a better solution. Thanks for your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants