Skip to content

Validator cache and closure #19937

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

Closed
hlecorche opened this issue Sep 14, 2016 · 4 comments
Closed

Validator cache and closure #19937

hlecorche opened this issue Sep 14, 2016 · 4 comments

Comments

@hlecorche
Copy link
Contributor

In validator component, a metadata object can have a closure.

But when framework.validation.cache is used with validator.mapping.cache.doctrine.apc, the cache throws an exception because cache can't seraliaze a closure.

Uncaught PHP Exception Exception: "Serialization of 'Closure' is not allowed" at /path/vendor/doctrine/cache/lib/Doctrine/Common/Cache/ApcCache.php line 57 {"exception":"[object] (Exception(code: 0): Serialization of 'Closure' is not allowed

@hlecorche
Copy link
Contributor Author

The exception is thrown when Callback validator is used with loadValidatorMetadata method :

    /**
     * @Assert\Callback
     */
    public function validate(ExecutionContextInterface $context, $payload)
    {
        //It's OK !

        $fakeNames = array('fake-name');

        if (in_array($this->getFirstName(), $fakeNames)) {
            $context->buildViolation('This name sounds totally fake!')
                ->atPath('firstName')
                ->addViolation();
        }
    }

    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        //Exception is thrown !!!!

        /*
         * Uncaught PHP Exception Exception: "Serialization of 'Closure' is not allowed" at
         * /path/vendor/doctrine/cache/lib/Doctrine/Common/Cache/ApcCache.php line 57 {"exception":"[object]
         * (Exception(code: 0): Serialization of 'Closure' is not allowed
         */

        $callback = function ($object, ExecutionContextInterface $context, $payload) {
            // ...
        };

        $metadata->addConstraint(new Assert\Callback($callback));
    }

You can test here : https://github.com/hlecorche/bug-closure (homepage route in prod environment)

@Koc
Copy link
Contributor

Koc commented Sep 21, 2016

looks like relates to #12302, #10083

@stof
Copy link
Member

stof commented Sep 30, 2016

if you want your metadata to be cacheable, don't use a closure for the callback.

@xabbuh
Copy link
Member

xabbuh commented Jan 13, 2017

Closing here as #20793 has been merged. Please comment with some more details if you still experience any issues.

@xabbuh xabbuh closed this as completed Jan 13, 2017
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

6 participants