Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

How use Callback validator when field is conditionally required? #146

@vincequeiroz

Description

@vincequeiroz

I have two fields and one of them is required if the value of the other is equal to one condition.

How can I do this?

The "campaignFranchise" field is only required when the value of the "campaign" is equal to "franchise".

            4 => [
                'required' => true,
                'validators' => [
                    0 => [
                        'name' => NotEmpty::class,
                        'options' => [...],
                    ],
                    1 => [
                        'name' => StringLength::class,
                        'options' => [...],
                    ],
                ],
                'filters' => [...],
                'name' => 'campaign',
            ],
            5 => [
                'required' => true,
                'validators' => [
                    0 => [
                        'name' => NotEmpty::class,
                        'options' => [
                            'translatorenabled' => true,
                        ],
                        'break_chain_on_failure' => true,
                    ],
                    1 => [
                        'name' => Callback::class,
                        'options' => [
                            'callback' => [
                                CampaignFranchise::class,
                                'isValid',
                            ],
                        ],
                    ],
                    2 => [
                        'name' => StringLength::class,
                        'options' => [...],
                    ],
                ],
                'filters' => [...],
                'name' => 'campaignFranchise',
            ],

I try use "continueIfEmpty" and "allowEmpty" but how I don't have value and is required, the validation return false.
https://github.com/zendframework/zend-inputfilter/blob/master/src/Input.php#L410-415

If the field is required false, the validation does not trigger.
https://github.com/zendframework/zend-inputfilter/blob/master/src/BaseInputFilter.php#L251-L256

And if I use the callback validator in "campaign" field, the message error result display in wrong field.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions