如果'全部'禁用其他选项选项在Sonata Admin configureFormFields中选择

时间:2017-05-10 09:56:11

标签: php symfony jquery-select2 sonata-admin symfony-sonata

在我的选择选项中,All是第一个选项。

  • 如果'全部'如何禁用所有其他字段?选项已选中
  • 反之亦然,如果选择了其他任何字段,则禁用'全部' 选项。 (可选)

Image

PushBroadcastAdmin.php

protected function configureFormFields(FormMapper $formMapper)
{
    if ($this->getSubject()->getId()) {
        $data = $this->getSubject()->getUsers();
        $recipientsArray = explode(',', $data);
    }
    else {
        $recipientsArray = [];
    }

    $em = $this->modelManager->getEntityManager('AppBundle:User');
    $users = $em->getRepository('AppBundle:User')->findAllUsers();


    $formMapper
        ->add('recipients', ChoiceType::class, array(
            'choices'  => $users,
            'data'     => $recipientsArray,
            'expanded' => false,
            'multiple' => true,
            'label'    => 'Recipient(s)',
            'attr'     => array(
                'class' => 'push_recipients_select',
            ),
        ))
        ->add('title')
        ->add('message')
    ;
}   

public function prePersist($object)
{
    $data = $object->getRecipients();
    $object->setUsers(implode(',',$data));
    parent::prePersist($object);

}

UserRepository.php

public function findAllUsers()
{
    $qb = $this->createQueryBuilder('u');
    $data = $qb
        ->select('u.id')
        ->addSelect('u.name')
        ->andWhere('u.enabled = 1')
        ->andWhere('u.isDeleted = 0')
        ->getQuery()
        ->getArrayResult();

    $result = [];
    $result['All'] = 'all';
    foreach ($data as $value){
        $result[$value['name']] = $value['id'];
    }

    return $result;
}

0 个答案:

没有答案