Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Clear that Location Name Data

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

When we change to the solar system, great! It loads the planets. We can even change to "Interstellar Space" and it disappears. We're amazing! And when we change it to "Choose a Location"... uhhh oh! Nothing happened? Ah, the Ajax part of the web debug toolbar is trying to tell me that there was a 500 error!

By the way, this is one of the coolest features of the web debug toolbar: when you get a 500 error on an AJAX call, you can click this link to jump straight into the profiler for that request! It takes us straight to the Exception screen so we can see exactly what we messed up, I mean, what went wrong... that may or may not be our fault.

Fixing our Empty Value Bug

Apparently ArticleFormType line 125 has an undefined "empty string" index. Let's go check that out. This is the method that we call to get the correct specificLocationName choices. But, in this case, the location is an empty string, and that's a super not valid key.

To fix this, add ?? null. This says, if the location key is set, use it, else use null.

... lines 1 - 18
class ArticleFormType extends AbstractType
{
... lines 21 - 119
private function getLocationNameChoices(string $location)
{
... lines 122 - 148
return $locationNameChoices[$location] ?? null;
}
}

Let's make sure that worked: on your browser, switch back to the solar system, and then back to "Choose a Location". Nice! The field disappears and no 500 error this time.

Forcing specificLocationName to null

There's one other subtle problem with our setup. To see it, refresh this page. In the database, this article's location is star, specificLocationName is Rigel and id is 28. Let's go verify this in the database: find your terminal and run:

php bin/console doctrine:query:sql 'SELECT * FROM article WHERE id = 28'

Yep! All the data looks like we expected! But now, change the location to "Interstellar Space" and hit update. It works... but try that query again:

php bin/console doctrine:query:sql 'SELECT * FROM article WHERE id = 28'

Ok: the location is interstellar_space, but ah! The specific_location_name is still Rigel! This may or may not be a real problem - depending on how you use this data. But it's for sure technically wrong: when we change the location to interstellar_space, the specific_location_name should be set back to null: we are not at Rigel.

The reason this did not happen is subtle. When we change the location to "Interstellar Space" and submit, our POST_SUBMIT listener function calls setupSpecificLocationNameField(), which sees that there are no choices for this location and so removes the field entirely. The end result is that the form makes no changes to the specificLocationName property on Article: it just never calls setSpecificLocationName() at all... because that field isn't part of the form!

That is the correct behavior. But, it means that we need to do a little bit more work to clean things up. There are a few ways to fix this inside the form itself. But, honestly, they're overly-complex. The solution I like lives entirely in Article. Open that class and find the setLocation() method. Inside, if there is no location, or if the location equals interstellar_space, call $this->setSpecificLocationName(null).

... lines 1 - 17
class Article
{
... lines 20 - 285
public function setLocation(?string $location): self
{
... lines 288 - 289
if (!$this->location || $this->location === 'interstellar_space') {
$this->setSpecificLocationName(null);
}
... lines 293 - 294
}
... lines 296 - 307
}

Simple! Oh, and in a real app, I'd probably add some class constants in Article to represent these special location keys so we could use something like Article::INTERSTELLAR_SPACE instead of just the string interstellar_space.

Let's try this people! First change the data back to a planet. Then, change it to "Interstellar Space" and update. Cool! Spin back over to our terminal and run that same query:

php bin/console doctrine:query:sql 'SELECT * FROM article WHERE id = 28'

Now it's set to null. Awesome. Next: we're pretty much done! There's just one last piece of homework left - and it's related to securing one of our endpoints.

Leave a comment!

2
Login or Register to join the conversation
Deuklyoung K. Avatar
Deuklyoung K. Avatar Deuklyoung K. | posted 4 years ago

Hi?

I think that the finish folder of this Course Code where is on the top and the right side doesn't include this chapter coding file.

Could you check that?

Reply

Hey Deuklyoung K.

You were right! The finish directory was missing some code. I just fixed it :)

Thanks for informing us about this problem. Cheers!

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.1.3",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
        "knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
        "knplabs/knp-time-bundle": "^1.8", // 1.8.0
        "nexylan/slack-bundle": "^2.0,<2.2.0", // v2.0.0
        "php-http/guzzle6-adapter": "^1.1", // v1.1.1
        "sensio/framework-extra-bundle": "^5.1", // v5.2.1
        "stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
        "symfony/asset": "^4.0", // v4.1.6
        "symfony/console": "^4.0", // v4.1.6
        "symfony/flex": "^1.0", // v1.17.6
        "symfony/form": "^4.0", // v4.1.6
        "symfony/framework-bundle": "^4.0", // v4.1.6
        "symfony/orm-pack": "^1.0", // v1.0.6
        "symfony/security-bundle": "^4.0", // v4.1.6
        "symfony/serializer-pack": "^1.0", // v1.0.1
        "symfony/twig-bundle": "^4.0", // v4.1.6
        "symfony/validator": "^4.0", // v4.1.6
        "symfony/web-server-bundle": "^4.0", // v4.1.6
        "symfony/yaml": "^4.0", // v4.1.6
        "twig/extensions": "^1.5" // v1.5.2
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.0", // 3.0.2
        "easycorp/easy-log-handler": "^1.0.2", // v1.0.7
        "fzaninotto/faker": "^1.7", // v1.8.0
        "symfony/debug-bundle": "^3.3|^4.0", // v4.1.6
        "symfony/dotenv": "^4.0", // v4.1.6
        "symfony/maker-bundle": "^1.0", // v1.8.0
        "symfony/monolog-bundle": "^3.0", // v3.3.0
        "symfony/phpunit-bridge": "^3.3|^4.0", // v4.1.6
        "symfony/profiler-pack": "^1.0", // v1.0.3
        "symfony/var-dumper": "^3.3|^4.0" // v4.1.6
    }
}
userVoice