3

In my select options All is the first option.

  • How do I disable all other fields if 'All' option is selected
  • And vice versa if any of other fields are selected disable 'All' option. (Optional)

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;
}
Zuhayer Tahir
  • 1,798
  • 3
  • 13
  • 20

0 Answers0