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

[DX] Form data may be empty in an unexpected way #75

Open
Nek- opened this issue Mar 30, 2024 · 0 comments
Open

[DX] Form data may be empty in an unexpected way #75

Nek- opened this issue Mar 30, 2024 · 0 comments

Comments

@Nek-
Copy link
Member

Nek- commented Mar 30, 2024

Explanation

When a property in the constructor is not filled and the form type has no validation error related, empty data auto-fill may fail silenciously.

SwagIndustries\Melodiia\Doctrine\DoctrineDataStore::save(): Argument #1 ($model) must be of type object, null given, called in /home/manager-app/vendor/swag-industries/melodiia/src/Crud/Controller/Create.php on line 68

Example

Let's take as an example this registration form:

use SwagIndustries\Melodiia\Form\AbstractType;

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', TextType::class, [
                'constraints' => [new NotBlank(), new Email()],
            ])
            ->add('password', TextType::class, ['constraints' => new NotBlank()])
            ->add('username', TextType::class) // optional
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefault('data_class', User::class);
    }
}

With the following User model:

class User
{
public function __construct(
    private string $email, 
    private string $password, 
    private string $username // Required!!
) {}
}

If you provide the following data to the API:

{
  "email": "[email protected]",
  "password": "somepass"
}

Then you will end up with a super weird (but normal because a parameter is missing) 500 error, instead of a validation error.

Solution

I have no out of the fryer option for now. Adding a log would be nice for sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant