Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Fields on some Pages, not Others

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

As we discussed earlier, configureFields() controls how each field is rendered on both the list page and the form pages. That leaves us with a situation that... isn't exactly "ideal". For example, we don't want an ID field on our form. But I do like having it on the index page!

To fix this, there are a bunch of useful methods on these field classes that we can utilize. For instance, we can call ->onlyOnIndex():

... lines 1 - 13
class UserCrudController extends AbstractCrudController
{
... lines 16 - 20
public function configureFields(string $pageName): iterable
{
yield IdField::new('id')
->onlyOnIndex();
... lines 25 - 29
}
}

And... just like that, it's gone from the form page, but we still have it on the index page. As you're playing with these methods, I invite you to be curious: dive in and check out the code behind the scenes. It's a great way to learn more about how EasyAdmin works on a deeper level.

Methods like ->onlyOnIndex() give us a lot of control. But also notice that configureFields() is passed the $pageName, which will be a string like index, detail, or edit. So in the end, you can always just put if statements inside of this method and conditionally yield - or don't yield - different fields.

Hiding on the Form

The other problem on our form is that we have this fullName field. In the database, we have firstName and lastName fields. It is kind of nice to render them as "Full Name" on the index page. But ultimately, when we go to the form, we really need separate firstName and lastName fields.

And, at the moment, this doesn't even work! If I change something on the form and submit... error! It says:

Could not determine access type for property fullName...

This is because, inside of our User class, we have a getFullName() method, but we do not have a setFullName() method (and I don't really want one). The point is that, over inside configureFields(), we need to change fullName to render ->onlyOnIndex():

... lines 1 - 13
class UserCrudController extends AbstractCrudController
{
... lines 16 - 20
public function configureFields(string $pageName): iterable
{
... lines 23 - 25
yield TextField::new('fullName')
->onlyOnIndex();
... lines 28 - 30
}
}

Now we'll have "Full Name" on our index, but we won't have one on the form.

And actually, instead of ->onlyOnIndex(), we can use ->hideOnForm():

... lines 1 - 13
class UserCrudController extends AbstractCrudController
{
... lines 16 - 20
public function configureFields(string $pageName): iterable
{
... lines 23 - 25
yield TextField::new('fullName')
->hideOnForm();
... lines 28 - 30
}
}

What's the difference? Using ->hideOnForm() still allows "Full Name" to show on the detail page. If I go back to "Users" and click "Show"... there it is!

Now that "Full Name" is gone from the form, let's put "First Name" and "Last Name" back. So, yield Field::new('firstName')... copy this, paste, and replace firstName with lastName:

... lines 1 - 13
class UserCrudController extends AbstractCrudController
{
... lines 16 - 20
public function configureFields(string $pageName): iterable
{
... lines 23 - 27
yield Field::new('firstName');
yield Field::new('lastName');
... lines 30 - 32
}
}

If we refresh... looks good! Over on the list page... looks weird! We don't want those here.

But now, we know what to do. There's a nice method for this: ->onlyOnForms(). Copy that, repeat it for lastName:

... lines 1 - 13
class UserCrudController extends AbstractCrudController
{
... lines 16 - 20
public function configureFields(string $pageName): iterable
{
... lines 23 - 27
yield Field::new('firstName')
->onlyOnForms();
yield Field::new('lastName')
->onlyOnForms();
... lines 32 - 34
}
}

And now... perfect!

Finally, let's do something similar for "Created At". I like having this on the list, but I do not like having it inside the form because it should be set automatically. So, down here, add ->hideOnForm():

... lines 1 - 13
class UserCrudController extends AbstractCrudController
{
... lines 16 - 20
public function configureFields(string $pageName): iterable
{
... lines 23 - 33
yield DateField::new('createdAt')
->hideOnForm();
}
}

Beautiful!

Next, I want to dive a bit further into fields. We're going to take one of these fields and configure its form type in a different way. As we do, we're going to accidentally learn about an important concept called field configurators.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=8.0.2",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "^1.11", // 1.11.99.4
        "doctrine/doctrine-bundle": "^2.1", // 2.5.5
        "doctrine/doctrine-migrations-bundle": "^3.0", // 3.2.1
        "doctrine/orm": "^2.7", // 2.10.4
        "easycorp/easyadmin-bundle": "^4.0", // v4.0.2
        "handcraftedinthealps/goodby-csv": "^1.4", // 1.4.0
        "knplabs/knp-markdown-bundle": "dev-symfony6", // dev-symfony6
        "knplabs/knp-time-bundle": "^1.11", // 1.17.0
        "sensio/framework-extra-bundle": "^6.0", // v6.2.5
        "stof/doctrine-extensions-bundle": "^1.4", // v1.7.0
        "symfony/asset": "6.0.*", // v6.0.1
        "symfony/console": "6.0.*", // v6.0.2
        "symfony/dotenv": "6.0.*", // v6.0.2
        "symfony/flex": "^2.0.0", // v2.0.1
        "symfony/framework-bundle": "6.0.*", // v6.0.2
        "symfony/mime": "6.0.*", // v6.0.2
        "symfony/monolog-bundle": "^3.0", // v3.7.1
        "symfony/runtime": "6.0.*", // v6.0.0
        "symfony/security-bundle": "6.0.*", // v6.0.2
        "symfony/stopwatch": "6.0.*", // v6.0.0
        "symfony/twig-bundle": "6.0.*", // v6.0.1
        "symfony/ux-chartjs": "^2.0", // v2.0.1
        "symfony/webpack-encore-bundle": "^1.7", // v1.13.2
        "symfony/yaml": "6.0.*", // v6.0.2
        "twig/extra-bundle": "^2.12|^3.0", // v3.3.7
        "twig/twig": "^2.12|^3.0" // v3.3.7
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.1
        "symfony/debug-bundle": "6.0.*", // v6.0.2
        "symfony/maker-bundle": "^1.15", // v1.36.4
        "symfony/var-dumper": "6.0.*", // v6.0.2
        "symfony/web-profiler-bundle": "6.0.*", // v6.0.2
        "zenstruck/foundry": "^1.1" // v1.16.0
    }
}