Skip to content

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

@Nek-

Description

@Nek-

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": "acme@swag.industries",
  "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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions