-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] InvalidArgumentException while using type declaration in entity #43509
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
Comments
Either use $builder
->add('name', TextType::class, ['empty_data' => ''])
; … or make the type declaration nullable. |
This is absolutely valid. You never know what data will be send in a request. Form Component maps the raw data to your model and when some field does not exist null value is passed so it can be validated against NotNull/NotBlank assert. You have to make you setters nullable as derrabus said or workaround with empty_data but then you will not know if a user didn't send this field or sent an empty string (but if you don't care it doesn't make any difference). However this hack with empty_data may work for string typehint but if you typed with some object type you could make it only nullable (ok, in PHP8 we have union types but it's better to avoid them in the userland code, especially in data objects). Btw. you should not use entities as your models in forms. You should use some kind of DTO so Form Component can populate it, the validate it and then if it is valid you can create your entity from this DTO. |
This would work only on string and I also have integer and boolean.
The maker bundle contain an entity generator, I tested and when I ask a field with not nullable string, the entity is generated with A test entity generated with
|
Hello, I just want to link this PR because it's somehow the same work to achieve but on the form component. #42502 |
This is not a duplication. This is a separation between your business logic represented by entity and raw data on data transfer objects that can be everything. Entities are part of your domain logic and should be in a valid state always while form data is just an input to the infrastructure layer which should be validated and if valid then your entity can be updated. I did use Api Platform a few years ago in few small applications and IIRC this is pretty simple to setup up a crud application using entities as Api Resource. Moving resources to models and writing custom persisters is the next level that also takes more time but solves many other issues and is just a better design. |
@javaDeveloperKid I'm sorry, but this is just not reasonable, no one uses Symfony like this and you can't expect people to duplicate their code just for this (look at the code examples and the Symfony documentation on how to use Forms and Entities). This is definitely a design issue of how the Forms and the Validator components work with the latest versions of PHP. We're struggling with this right now, and this just invalidates the entire point of using types, if we need to declare them as nullable. |
@danieldenbraven
As you see in step no. 3 the setters must be nullable because you never know what data will come from outside world. About the rest of your commentNo one uses Symfony like this? You mean no junior developers, right? I explained why this is not considered a duplication. No senior developer will map their entities to form models. One first must validate the input data and then, when valid, pass this data further in process chain. |
Actually he does, and it's the error (TypeError). About your idea of just stopping using forms but doing it with API Platform, I really do not understand your point of view for 3 reasons:
Besides, as a reminder, the quick fix has been given: use the |
I agree that the DX could be nicer here. But somebody has to build that. Any volunteers? 🙂 |
In the RichModelFormsBundle we among other use cases also deal with type errors when mapping user input from the input data to the model. Anyone wanting to improve DX here please feel free to take inspiration from the code there. |
Closing here as this is not a bug, but related to how the Form component works. As @derrabus said we are nonetheless open to review PRs improving DX around this. |
Symfony version(s) affected: 5.3.4
Description
I use PHP 8 and my entities use the type declaration on setter and getter.
When validating a Form with a
null
value on a field that require non null (eg:method(string $value)
), thehandle()
method throw anInvalidArgumentException
and aTypeError
.How to reproduce
Example Entity, Form and Controller
src/Entity/Structure.php
src/Form/StructureType.php
src/Controller/Structure.php
Possible Solution
Catch these exceptions and add a validation error asking a non null value (or use the existing Validator on the field)
Additional context
The stack trace when
$form->handleRequest($request)
is calledThe text was updated successfully, but these errors were encountered: