-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Comments
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. |
#6102 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();
}
} |
Not the same 😄 - you need just disable it. ->setDisabled($this->adminProvider->getContext()->getEntity()->getInstance()?->isSubmitted() ?? false) |
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.
The text was updated successfully, but these errors were encountered: