Skip to content

PropertyAccessor::getReadAccessInfo() throws exception that could be more specific #46867

Closed
symfony/form
#32
@patrickmaynard

Description

@patrickmaynard

Symfony version(s) affected

5,6, possibly others

Description

Currently, if a form field does not have an equivalent setter or adder and does not use the ['mapped' => false] option, we get a NoSuchPropertyException that complains about a lack of setters or adders when submitting the form.

(This is Symfony\Component\PropertyAccess\PropertyAccessor::getReadAccessInfo(), in the method's final else branch.)

That's fine, I guess, but it can be a little bit confusing. Would it be safe for me to open a pull request adding a reference to ['mapped' => false] as a possible fix in the text of the exception? Or is this method used in non-form contexts?

How to reproduce

Create a form field in a form type that doesn't have a setter or adder associated with it in the equivalent Doctrine entity, and don't pass any options to the field. Then submit the form. You should get a nice NoSuchPropertyException talking about the missing methods, but it won't mention anything about how to solve the issue if you don't want to add those methods.

Possible Solution

I'm thinking about opening a pull request to change this part of Symfony\Component\PropertyAccess\PropertyAccessor::getReadAccessInfo() ...

$access[self::ACCESS_NAME] = sprintf(
    'Neither the property "%s" nor one of the methods "%s()" '.
-    'exist and have public access in class "%s".',
+    'exist and have public access in class "%s".' Consider adding one of those methods, or if you don't want a direct mapping, use ['mapped' => false] as a form field option.', 
    $property,
    implode('()", "', $methods),
    $reflClass->name
);

... but I first need to know that it's safe for me to do so without accidentally adding misleading instructions about form options to contexts that don't involve forms.

Please comment!

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions