PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 10, 2022

[FIXED] EasyAdmin choiceField bug Symfony

 February 10, 2022     choicefield, crud, easyadmin, roles, symfony     No comments   

Issue

I think there's a bug in EasyAdmin 3.5.9, but before creating an issue in GitHub, I would like to have people's opinion.

In my User CRUD, I wanted to be able to edit the user role, which wasn't displying by default. So I added it like so in UserCrudController class:

public function configureFields(string $pageName): iterable
{
    $roles = [
        'User'   => 'ROLE_USER',
        'Agency' => 'ROLE_AGENCY',
        'Admin'  => 'ROLE_ADMIN'
    ];

    return [
        // ...
        ChoiceField::new('roles')
            ->setChoices($roles)->allowMultipleChoices(),
        // ...
    ];
}

Here is how it looks in User entity:

/**
 * @see UserInterface
 */
public function getRoles(): array
{
    $roles = $this->roles;

    $roles[] = 'ROLE_USER';
    $roles[] = 'ROLE_AGENCY';
    $roles[] = 'ROLE_ADMIN';

    return array_unique($roles);
}

public function setRoles(array $roles): self
{
    $this->roles = array_values($roles);

    return $this;
}

Actually, when I want to edit a user with EasyAdmin by selecting one of these choices, it save the right value in database. But when I come back on the same user to edit it, still all choices are displaying in the field, like I did nothing before. So it seems to be more a displaying issue than a data saving issue.

Here is a quick screen record to understand better: https://watch.screencastify.com/v/If9xScwQAFqcbbfGBmza

Hope you can help me.


Solution

Your UserInterface::getRoles() is manually adding all the roles back in with the set roles. The method should only need the following:

public function getRoles(): array
{
    return array_unique($this->roles);
}


Answered By - Arleigh Hix
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing