Skip to content

[Form][Validation] Unique entity validator throw undefined index if no data is provided #23486

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
flohw opened this issue Jul 12, 2017 · 10 comments

Comments

@flohw
Copy link
Contributor

flohw commented Jul 12, 2017

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version >=2.7.25

The UniqueEntityValidator throw an "undefined index error" when submitting no data or null value on a defined entity's field.

My entity has UniqueEntity(fields={"reference", "supplier"}) constraint.
In my functional test I submit an empty array as post data.
The response is "Undefined index: reference".

After some research, the bug was introduced here symfony/doctrine-bridge@40941a3#diff-e59b775599f36ee1318299634bc1be79 when line 88 has been splitted.
In my exemple $criteria['reference'] does not exist any more and the error (undefined index) occurs on line 137
If $criteria[$fieldName] = $fieldValue; were between line 88 and 90 it would works properly.

Sorry but I am not sure about the solution and don't know how to create a test for this case. If someone can help me about that.

Thank you.

Curiosity question : is there a reason to use ClassMetdata attributes (like reflFields) to get field values instead of getFieldValue($entity, $field) method?

@xabbuh
Copy link
Member

xabbuh commented Jul 12, 2017

I am not sure how to reproduce your issue. Can you show the functional test that is triggering this?

@xabbuh
Copy link
Member

xabbuh commented Jul 12, 2017

Can you please also investigate what exactly gets passed to UniqueEntityValidator::validate() for the $entity argument in your case?

@flohw
Copy link
Contributor Author

flohw commented Jul 13, 2017

Hello,
The error is trigger when calling $client->request() :

$client->request(
            'PUT',
            $this->getUrl('api_put_article', ['id' => 1]),
            [],
            [],
            ['CONTENT_TYPE' => 'application/json'],
            json_encode([]) // Empty array submitted
        );

The parameter $entity is the expected entity (the Article object).
Both properties exist : supplier and reference. supplier is unchanged compared to the fixture, reference is set to null as no data is provided.

@xabbuh
Copy link
Member

xabbuh commented Jul 13, 2017

I am sorry, but I still cannot reproduce your issue. Did you try the latest patch releases (line 137 that you mentioned in the issue description isn't the same in more recent releases)? If you still experience the issue then, can you please create a small example project that allows to reproduce your issue?

@wuestkamp
Copy link

wuestkamp commented Jul 14, 2017

Same issue here, Symfony 2.8.17

I get this issue when I do not submit the field in my fields-array of my unique entity annotation. So when its disabled in the form because its not needed in a case.

@xabbuh
Copy link
Member

xabbuh commented Jul 14, 2017

Well, it would still be nice to have a small project that would allow to reproduce the issue.

@flohw
Copy link
Contributor Author

flohw commented Jul 17, 2017

Hi !

Sorry for the delay, day off and week-end... :-)

Here is a code reproducing the error : https://github.com/flohw/form_errors/
Parameters for mysql connexion are already defined in parameters.yml.dist.

docker-compose build
docker-compose up -d
docker-compose exec php app/console doctrine:schema:create

On / submit the form with 'test' as value. It works. Re submit with 'test' as value then it fails.

Symfony version : 2.7.18 (installed with symfony installer, same as my original project).
I tried then to update with composer (version set to 2.8.* in composer.json) but composer says "Nothing to install or update".

@flohw
Copy link
Contributor Author

flohw commented Jul 24, 2017

Hi ! Is there some updates here ?

@xabbuh
Copy link
Member

xabbuh commented Jul 24, 2017

@flohw Sorry for not replying earlier. I have tried your project and the issue indeed is that you are using Symfony 2.8.17. Updating to 2.8.18 (and thus getting the changes from #21562 resolves it). I am closing here as the issue is already fixed.

By the way, the fact that you were not able to update to 2.8.18 is related to the fact that your PHPUnit requirement is bound to 4.1.*.

@xabbuh xabbuh closed this as completed Jul 24, 2017
@flohw
Copy link
Contributor Author

flohw commented Jul 24, 2017

Ok, I'll try to update phpunit too. Thanks for the hint ! :-)

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

4 participants