8000 Error FormTypeTest : Call to a member function addConstraint() on null · Issue #20952 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Error FormTypeTest : Call to a member function addConstraint() on null #20952

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
GCalmels opened this issue Dec 16, 2016 · 5 comments
Closed

Comments

@GCalmels
Copy link
Q A
Bug report? yes
Feature request? no
BC Break report? no
Symfony version 2.8.15

I followed the doc for adding custom extensions and I had an error at new ValidatorExtension($this->validator),

The cause of this error is on the _construct of ValidatorExtension.php :
$metadata = $validator->getMetadataFor('Symfony\Component\Form\Form');
and after :
$metadata->addConstraint(new Form()); but $metadata is null

@xabbuh
Copy link
Member
xabbuh commented Dec 16, 2016

@GCalmels I think that's an issue with the example provided in the documentation, but not related to the Symfony code. Can open a new issue on the docs repository?

@GCalmels
Copy link
Author
8000

Well, do you know the valid example? Yes, I will open a new issue!

@xabbuh
Copy link
Member
xabbuh commented Dec 16, 2016

The mocked ValidatorInterface would have to return a valid constraint (and you will probably have to mock some more stuff). Though not completely sure if that's really the best way to go or if we shouldn't rewrite the whole example.

@GCalmels
Copy link
Author

I post the solution I found :

protected function getExtensions()
    {
        $this->validator = $this->createMock(ValidatorInterface::class);
        $this->validator
            ->method('validate')
            ->will($this->returnValue(new ConstraintViolationList()));
        $this->validator
            ->method('getMetadataFor')
            ->will($this->returnValue(new ClassMetadata('Symfony\Component\Form\Form')));

        return array(
            new ValidatorExtension($this->validator)
        );
    }

@xabbuh
Copy link
Member
xabbuh commented Dec 16, 2016

@GCalmels Thank you! Can you please also add this as a comment to symfony/symfony-docs#7270.

xabbuh added a commit to symfony/symfony-docs that referenced this issue Jun 3, 2017
This PR was merged into the 2.7 branch.

Discussion
----------

added missing ClassMetadata

This needs to be merged into the following branches too

Closes #7270
Refs symfony/symfony#20952

Commits
-------

c193035 added missing ClassMetadata
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

2 participants
0