Symfony3 Form component trying to pass null to a type hinted method in PHP 7 -


in entity class have defined expected argument types setters , return types of getters. later, when have form uses said class, error if of fields in form empty because form component tries pass null setter instead of string.

i following exception when submit form:

expected argument of type "string", "null" given

500 internal server error - invalidargumentexception

the exception thrown vendor/symfony/symfony/src/symfony/component/propertyaccess/propertyaccessor.php @ line 254

is there way convert "null" value empty string before passing object, , let validator argue it?

i see 2 options here:

quick , dirty - make argument passed setter optional:

public function settitle(string $title = null) {     $this->title = $title;     return $this; } 

probably better - use data transformer in formtype:

data transformers allow modify data before gets used.

   $builder     // ...         ->add('title', 'text')     // ...     ;      $builder->get('title')->addmodeltransformer(new callbacktransformer(         function($originalinput){             return $string;         },         function($submittedvalue){              // when null cast string, empty.             return (string) $submittedvalue;         }     )); 

i've posted another answer before using retrieve method retrieve entity object before. see if helps see more complicated example.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo