Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active June 24, 2016 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/8b7e874340823278ffcbe7face9517d2 to your computer and use it in GitHub Desktop.
Save webdevilopers/8b7e874340823278ffcbe7face9517d2 to your computer and use it in GitHub Desktop.
Using PHP7 type hinting with Symfony Form Text Type and empty_data returning NULL
<?php
class ChangeInspectionDetails extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('problemNumber', TextType::class, [
'empty_data' => '' // no effect
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => ChangeInspectionDetailsCommand::class
]);
}
}
<?php
class ChangeInspectionDetailsCommand
{
public $problemNumber = ''; // no effect
public function problemNumber()
{
return (string) $this->problemNumber; // workaround 1
}
}
<?php
class ChangeInspectionDetailsHandler
{
public function handle(ChangeInspectionDetailsCommand $command)
{
// ...
$contract->changeInspectionDetails(
#$command->problemNumber() // no effect
(string) $command->problemNumber() // workaround 2
);
}
}
<?php
class Contract
{
public $problemNumber = ''; // no effect
public function changeInspectionDetails(
string $problemNumber = ''
) {
$this->problemNumber = $problemNumber;
}
}
@webdevilopers
Copy link
Author

Related issue: webdevilopers/php-ddd#18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment