Restrict user fields in UserGroupType based on user jobs.

Refactored the form to conditionally add 'users' and 'adminUsers' fields only if the UserGroup does not have associated user jobs. This ensures data consistency and adjusts the form behavior dynamically based on the entity state.
This commit is contained in:
Julien Fastré 2025-04-25 13:10:44 +02:00
parent fb1c34f9c1
commit e933f3e781
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Form; namespace Chill\MainBundle\Form;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Form\Type\PickUserDynamicType; use Chill\MainBundle\Form\Type\PickUserDynamicType;
use Chill\MainBundle\Form\Type\TranslatableStringFormType; use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
@ -23,6 +24,9 @@ class UserGroupType extends AbstractType
{ {
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
/** @var UserGroup $userGroup */
$userGroup = $options['data'];
$builder $builder
->add('label', TranslatableStringFormType::class, [ ->add('label', TranslatableStringFormType::class, [
'label' => 'user_group.Label', 'label' => 'user_group.Label',
@ -46,7 +50,10 @@ class UserGroupType extends AbstractType
'help' => 'user_group.ExcludeKeyHelp', 'help' => 'user_group.ExcludeKeyHelp',
'required' => false, 'required' => false,
'empty_data' => '', 'empty_data' => '',
]) ]);
if (!$userGroup->hasUserJob()) {
$builder
->add('users', PickUserDynamicType::class, [ ->add('users', PickUserDynamicType::class, [
'label' => 'user_group.Users', 'label' => 'user_group.Users',
'multiple' => true, 'multiple' => true,
@ -62,4 +69,6 @@ class UserGroupType extends AbstractType
]) ])
; ;
} }
}
} }