Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to disable fields based on entity property #6406

Open
nicklocking opened this issue Aug 9, 2024 · 3 comments
Open

No way to disable fields based on entity property #6406

nicklocking opened this issue Aug 9, 2024 · 3 comments

Comments

@nicklocking
Copy link

Describe the bug

Imagine you have an Order entity. It has a property named address (string) and submitted (bool). If the order is submitted=false, the address property can be edited. If the order is submitted=true, it cannot.

I can't work out a clean way of doing this with the existing EasyAdmin code. I can get to a point where I can optionally add fields to the edit form by hooking in to createEditFormBuilder, but they don't appear styled like EasyAdmin form fields so it feels like there must be a better way.

In the same way that Field::formatValue() takes a callable function, I would expect there to be some sort of Field::customizeField() method that takes a similar callable.

To Reproduce

Create entity as described above and then attempt to make a field's disabled property dynamic based on another entity property.

@a-r-m-i-n
Copy link
Contributor

In edit mode you can access $this->adminContext and from it the entity dto and the actual instance. Then you can change the configuration to your needs, based on given values, in configureFields method of your CrudController.

@zorn-v
Copy link
Contributor

zorn-v commented Aug 11, 2024

#6102
I have same problem and do it via security voter and setPermission on field.
Will not work on index page, but I just disabled that field there.

Something like this in my case

//in controller configureFields
Field::new('disabled')->setPermission('NOT_YOURSELF')->setDisabled(Crud::PAGE_INDEX === $pageName)
<?php

namespace App\Security\Voter;

use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;

class NotYourselfVoter extends Voter
{
    public const ATTR = 'NOT_YOURSELF';

    public function __construct(
        private AdminContextProvider $adminProvider,
    ) {}

    protected function supports(string $attribute, mixed $subject): bool
    {
        return $attribute === self::ATTR;
    }

    protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
    {
        $user = $token->getUser();
        // if the user is anonymous, grant access
        if (!$user instanceof UserInterface) {
            return true;
        }

        if (null === $entity = $this->adminProvider->getContext()->getEntity()->getInstance()) {
            return true;
        }

        return $entity->getUserIdentifier() !== $user->getUserIdentifier();
    }
}

@zorn-v
Copy link
Contributor

zorn-v commented Aug 11, 2024

I have same problem

Not the same 😄 - you need just disable it.
Then as @a-r-m-i-n said inject admin context and something like

->setDisabled($this->adminProvider->getContext()->getEntity()->getInstance()?->isSubmitted() ?? false)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants