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

Unclear exception message in Symfony\Component\PropertyAccess\Exception\AccessException; #36277

Closed
Holicz opened this issue Mar 30, 2020 · 0 comments

Comments

@Holicz
Copy link
Contributor

Holicz commented Mar 30, 2020

Symfony version(s) affected: >= 3.4

Description
When PHP 7.4 typed property is accessed in form before initialization Symfony throws an exception with a message The property "App\Form\Model\ContactFormModel::$firstName" is not readable because it is typed "string". You should either initialize it or make it nullable using "?string" instead. which is entirely not true.

If I have this in my model:

/**
 * @Assert\NotBlank
 */
public string $firstname;

And will make it this according to the message:

/**
 * @Assert\NotBlank
 */
public ?string $firstname;

The error is still there since I need to make it this

/**
 * @Assert\NotBlank
 */
public ?string $firstname = null;

How to reproduce
Create a model with below code, use it in a form, render the form and try to fix the error by the exception message instructions.

/**
 * @Assert\NotBlank
 */
public string $firstname;

Possible Solution
Improve the error message, I will provide the PR. What do you think about The property "App\Form\Model\ContactFormModel::$firstName" is not readable because it is typed "string". You should either initialize it or make it nullable using "?string" **and initialize it to null** instead.?

@Holicz Holicz added the Bug label Mar 30, 2020
@fabpot fabpot closed this as completed Apr 6, 2020
fabpot added a commit that referenced this issue Apr 6, 2020
… in php 7.4 (lmasforne)

This PR was squashed before being merged into the 3.4 branch.

Discussion
----------

[PropertyAccess] Improve message of unitialized property in php 7.4

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36277
| License       | MIT

Improve message of unitialized property in php 7.4 ;
Before
You should either initialize it or make it nullable using "?string" instead.
After
You should either initialize it or make it nullable using "?string $var = null" instead.

Commits
-------

3c8bf2d [PropertyAccess] Improve message of unitialized property in php 7.4
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