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

Validator with Zend\I18n\Validator\DateTime is always required #7

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

Comments

@weierophinney
Copy link
Contributor

I am attempting to use the DateTime validator in an Apigility project. The configuration is set to not be required and continue if empty, however the validator always returns a validation error.

Input

{"name": "Joe Public", "email": "joe@business.com"}

Validator configuration

1 => array(
    'name' => 'dateField',
    'required' => false,
    'filters' => array(),
    'validators' => array(
        0 => array(
            'name' => 'Zend\\I18n\\Validator\\DateTime',
            'options' => array(
                'pattern' => 'Y-m-d\TH:iP',
                'message' => 'Date format must be Y-m-d\TH:iP',
            ),
        ),
    ),
    'allow_empty' => true,
    'continue_if_empty' => true,
    'description' => 'Date field',
),

Error message

{
  "validation_messages": {
    "dateOfBirth": {
      "datetimeInvalid": "Date format must be Y-m-d\TH:iP"
    }
  },
  "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
  "title": "Unprocessable Entity",
  "status": 422,
  "detail": "Failed Validation"
}

Originally posted by @alexisbmills at zfcampus/zf-content-validation#49

@weierophinney
Copy link
Contributor Author

I cannot reproduce.

To attempt to reproduce, I added zend-i18n as a dev dependency (composer require --dev zendframework/zend-i18n), and then edited test/ContentValidationListenerTest.php to do the following:

  • Import Zend\I18n\Validator\DateTime
  • Add the following test case:
    public function testUnrequiredDateTimeValidatorShouldNotResultInInvalidationOfSet()
    {
        $services = new ServiceManager();
        $factory  = new InputFilterFactory();
        $services->setService('FooValidator', $factory->createInputFilter([
            'dateField' => [
                'name' => 'dateField',
                'required' => false,
                'validators' => [
                    [
                        'name' => DateTime::class,
                        'options' => [
                            'pattern' => 'Y-m-d\TH:iP',
                            'message' => 'Invalid date format',
                        ],
                    ],
                ],
                'allow_empty' => true,
                'continue_if_empty' => true,
                'description' => 'Date Field',
            ],
        ]));
        $listener = new ContentValidationListener(
            [
                'Foo' => [
                    'GET' => 'FooValidator',
                ],
            ],
            $services,
            ['Foo' => 'foo_id']
        );

        $request = new HttpRequest();
        $request->setMethod('POST');

        $matches = $this->createRouteMatch(['controller' => 'Foo', 'foo_id' => 3]);

        $dataParams = new ParameterDataContainer();
        $dataParams->setBodyParams([
            'name' => 'Joe Public',
            'email' => 'joe@business.com',
        ]);

        $event = new MvcEvent();
        $event->setRequest($request);
        $event->setRouteMatch($matches);
        $event->setParam('ZFContentNegotiationParameterData', $dataParams);

        $this->assertNull($listener->onRoute($event));
        $this->assertNull($event->getResponse());
    }

null is expected when validation passes; otherwise, it will return an ApiProblem.

The test passed.

Can you update your dependencies and report back if the problem still exists? If it does, we'll need more information.


Originally posted by @weierophinney at zfcampus/zf-content-validation#49 (comment)

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

No branches or pull requests

1 participant