13

In my entity class I have defined all the expected argument types for the setters and return types of the getters. Later, when I have a form which uses the said class, I get an error if some of the fields in the form is empty because the form component tries to pass null to the setter instead of string.

I get the following exception when I submit the form:

Expected argument of type "string", "NULL" given

500 Internal Server Error - InvalidArgumentException

The exception is thrown from vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php at line 254

Is there a way to convert the "null" value to empty string before passing it to the object, and let the validator argue about it?

2 Answers 2

11

If an Entity Property cannot be null (and you use PHP 7.1+), then also the application of the nullable return type declaration sounds more like a dirty and fast workaround to maintain a direct data binding between Entities and Forms (using the Symfony Form Component).

A better global approach (in my opinion) is to decouple the Form data binding from your Entities using a DTO (Data Transfer Object), that is a simple POPO (Plain Old PHP Object) to contain your form data.

Using a DTO will allow you to maintain a strict type hinting in your Entities (no loss of data consistency) and will decouple Form data binding (but also data validation) from your Entities.

DTO's allows reusability and have many other advantages.

Some useful references about the use of DTO's with Symfony Forms:

5

I see two options here:

Quick and Dirty - make the argument passed to the setter optional:

public function setTitle(String $title = null)
{
    $this->title = $title;
    return $this;
}

Probably better - use a data transformer in the FormType:

Data transformers allow you to modify the data before it gets used.

   $builder
    // ...
        ->add('title', 'text')
    // ...
    ;

    $builder->get('title')->addModelTransformer(new CallbackTransformer(
        function($originalInput){
            return $string;
        },
        function($submittedValue){ 
            // When null is cast to a string, it will be empty.
            return (string) $submittedValue;
        }
    ));

I've posted another answer before using this to retrieve method to retrieve an Entity object before. See that if it helps to see a more complicated example.

4
  • When using the second way, the data is transformed and the exception is not thrown, but the validation doesn't work anymore. I tried looking at the documentation but couldn't find why. The first option works, but as you said, it's kinda dirty.
    – Angelov
    Commented Jun 1, 2016 at 20:12
  • P.S. The validation doesn't work only on those fields that an data transformer is added. For example, if I have an "name" and "email" fields, and I add a data transformer to the "name" field, then if I leave it empty and put some invalid email address in the "email" field, there will be an validation error message only for the "email" field.
    – Angelov
    Commented Jun 1, 2016 at 20:28
  • 1
    @Angelov, Loud and clear. Unfortunately, its been a while since I've worked with data transformers and the validation component and I don't remember all the nuances with them; I'm not sure how I could help more than I have. It might be helpful for the question to see some of these validation rules though - if I recall correctly, there are multiple ways to use the validator.
    – HPierce
    Commented Jun 1, 2016 at 20:35
  • I added an issue on the PHPDDD project on github which offers a workaround at least for working with Commands using CQRS: github.com/webdevilopers/php-ddd/issues/18 Commented Jun 24, 2016 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.