Beispiel #1
0
 /**
  * {@inheritDoc}
  */
 protected function configure()
 {
     $this->addRequiredOption('choices');
     $this->addOption('preferred_choices', array());
     $this->addOption('separator', '----------');
     $this->addOption('multiple', false);
     $this->addOption('expanded', false);
     $this->addOption('empty_value', '');
     if (!is_array($this->getOption('choices'))) {
         throw new UnexpectedTypeException('The choices option must be an array');
     }
     if (!is_array($this->getOption('preferred_choices'))) {
         throw new UnexpectedTypeException('The preferred_choices option must be an array');
     }
     if (count($this->getOption('preferred_choices')) > 0) {
         $this->preferredChoices = array_flip($this->getOption('preferred_choices'));
     }
     if ($this->getOption('expanded')) {
         $this->setFieldMode(self::GROUP);
         $choices = $this->getOption('choices');
         foreach ($this->preferredChoices as $choice => $_) {
             $this->add($this->newChoiceField($choice, $choices[$choice]));
         }
         foreach ($choices as $choice => $value) {
             if (!isset($this->preferredChoices[$choice])) {
                 $this->add($this->newChoiceField($choice, $value));
             }
         }
     } else {
         $this->setFieldMode(self::FIELD);
     }
     parent::configure();
 }
Beispiel #2
0
 protected function configure()
 {
     $this->addRequiredOption('choices');
     $this->addOption('preferred_choices', array());
     $this->addOption('multiple', false);
     $this->addOption('expanded', false);
     $this->addOption('empty_value', '');
     parent::configure();
     $choices = $this->getOption('choices');
     if (!is_array($choices) && !$choices instanceof \Closure) {
         throw new InvalidOptionsException('The choices option must be an array or a closure', array('choices'));
     }
     if (!is_array($this->getOption('preferred_choices'))) {
         throw new InvalidOptionsException('The preferred_choices option must be an array', array('preferred_choices'));
     }
     if (count($this->getOption('preferred_choices')) > 0) {
         $this->preferredChoices = array_flip($this->getOption('preferred_choices'));
     }
     if ($this->isExpanded()) {
         $this->setFieldMode(self::FORM);
         $choices = $this->getChoices();
         foreach ($this->preferredChoices as $choice => $_) {
             $this->add($this->newChoiceField($choice, $choices[$choice]));
         }
         foreach ($choices as $choice => $value) {
             if (!isset($this->preferredChoices[$choice])) {
                 $this->add($this->newChoiceField($choice, $value));
             }
         }
     } else {
         $this->setFieldMode(self::FIELD);
     }
 }