public function configure() { $project = $this->getData(); $this->addRequiredOption('entity_manager'); $em = $this->getOption('entity_manager'); $this->add(new TextField('name')); $statusTransformer = new EntityToIDTransformer(array('em' => $em, 'className' => 'Application\\ChiaBundle\\Entity\\Status')); $statusField = new ChoiceField('status', array('choices' => $em->getRepository('Application\\ChiaBundle\\Entity\\Status')->getStatusOptions())); $statusField->setValueTransformer($statusTransformer); $this->add($statusField); $contactTransformer = new EntityToIDTransformer(array('em' => $em, 'className' => 'Application\\ChiaBundle\\Entity\\Contact')); $contactField = new AutocompleteField('contact', array('choices' => $em->getRepository('Application\\ChiaBundle\\Entity\\Contact')->getCompanyOptions())); $contactField->setValueTransformer($contactTransformer); $this->add($contactField); $userTransformer = new EntityToIDTransformer(array('em' => $em, 'className' => 'Application\\ChiaBundle\\Entity\\User')); $ownerField = new ChoiceField('owner', array('choices' => $em->getRepository('Application\\ChiaBundle\\Entity\\User')->getUserOptions())); $ownerField->setValueTransformer($userTransformer); $this->add($ownerField); $this->add(new TextareaField('description')); $this->add(new MoneyField('price')); $this->add(new ChoiceField('price_type', array('choices' => Project::$price_types))); $this->add(new DateField('estimated_start_date')); $this->add(new DateField('estimated_end_date')); $categoryTransformer = new EntityToIDTransformer(array('em' => $em, 'className' => 'Application\\ChiaBundle\\Entity\\Category')); $categoryField = new ChoiceField('category', array('choices' => $em->getRepository('Application\\ChiaBundle\\Entity\\Category')->getCategoryOptions(), 'empty_value' => 'Select a category...')); $categoryField->setValueTransformer($categoryTransformer); $this->add($categoryField); }
public function configure() { $this->addRequiredOption('entity_manager'); $em = $this->getOption('entity_manager'); $statusTransformer = new EntityToIDTransformer(array('em' => $em, 'className' => 'Application\\ChiaBundle\\Entity\\Status')); $statusField = new ChoiceField('status', array('choices' => $em->getRepository('Application\\ChiaBundle\\Entity\\Status')->getStatusOptions())); $statusField->setValueTransformer($statusTransformer); $this->add($statusField); $noteField = new TextareaField('new_note'); $this->add($noteField); }
public function __construct($key, array $options = array()) { $this->addOption('repository'); $this->setValueTransformer(new DoctrineObjectTransformer($options['repository'])); if (empty($options['choices'])) { $options['choices'] = $options['repository']->findAllIndexById(); } parent::__construct($key, $options); }
public function configure() { $this->addRequiredOption('entity_manager'); $em = $this->getOption('entity_manager'); $this->add(new TextField('task')); $this->add(new DateField('due_date')); $userTransformer = new EntityToIDTransformer(array('em' => $em, 'className' => 'Application\\ChiaBundle\\Entity\\User')); $ownerField = new ChoiceField('owner', array('choices' => $em->getRepository('Application\\ChiaBundle\\Entity\\User')->getUserOptions())); $ownerField->setValueTransformer($userTransformer); $this->add($ownerField); $categoryTransformer = new EntityToIDTransformer(array('em' => $em, 'className' => 'Application\\ChiaBundle\\Entity\\TaskCategory')); $categoryField = new ChoiceField('category', array('choices' => $em->getRepository('Application\\ChiaBundle\\Entity\\TaskCategory')->getCategoryOptions(), 'empty_value' => 'Select a category...')); $categoryField->setValueTransformer($categoryTransformer); $this->add($categoryField); $projectTransformer = new EntityToIDTransformer(array('em' => $em, 'className' => 'Application\\ChiaBundle\\Entity\\Project')); $projectField = new HiddenField('project'); $projectField->setValueTransformer($projectTransformer); $this->add($projectField); }
public function __construct($key, array $options = array()) { $data = unserialize(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'es.dat')); $countries = $data['Countries']; foreach ($countries as $idx => $country) { if (is_numeric($idx)) { unset($countries[$idx]); } else { break; } } if (!isset($options['choices'])) { $options['choices'] = $countries; } if (!isset($options['preferred_choices'])) { $options['preferred_choices'] = array('US', 'MX', 'CA'); } if (!isset($options['empty_value'])) { $options['empty_value'] = 'Choose a country...'; } parent::__construct($key, $options); }
/** * @inheritDoc */ protected function configure() { $this->addOption('choices', Locale::getDisplayLanguages($this->locale)); parent::configure(); }
/** * @dataProvider getNumericChoicesVariants */ public function testSubmitMultipleExpandedNumericChoices($choices) { $field = new ChoiceField('name', array('multiple' => true, 'expanded' => true, 'choices' => $choices)); $field->submit(array(1 => 1, 2 => 2)); $this->assertSame(array(1, 2), $field->getData()); $this->assertSame(false, $field[0]->getData()); $this->assertSame(true, $field[1]->getData()); $this->assertSame(true, $field[2]->getData()); $this->assertSame(false, $field[3]->getData()); $this->assertSame(false, $field[4]->getData()); $this->assertSame('', $field[0]->getDisplayedData()); $this->assertSame('1', $field[1]->getDisplayedData()); $this->assertSame('1', $field[2]->getDisplayedData()); $this->assertSame('', $field[3]->getDisplayedData()); $this->assertSame('', $field[4]->getDisplayedData()); $this->assertSame(array(0 => '', 1 => '1', 2 => '1', 3 => '', 4 => ''), $field->getDisplayedData()); }
protected function configure() { $this->addOption('choices', Locale::getDisplayCountries(\Locale::getDefault())); parent::configure(); }
public function testRenderMultipleExpanded() { $field = new ChoiceField('name', array('multiple' => true, 'expanded' => true, 'choices' => $this->choices)); $field->setData(array('a', 'b')); $html = <<<EOF <input id="name_a" name="name[a]" value="a" checked="checked" type="checkbox" /> <label for="name_a">Bernhard</label> <input id="name_b" name="name[b]" value="b" checked="checked" type="checkbox" /> <label for="name_b">Fabien</label> <input id="name_c" name="name[c]" value="c" type="checkbox" /> <label for="name_c">Kris</label> <input id="name_d" name="name[d]" value="d" type="checkbox" /> <label for="name_d">Jon</label> <input id="name_e" name="name[e]" value="e" type="checkbox" /> <label for="name_e">Roman</label> EOF; $this->assertEquals($html, $field->render()); }
/** * Transforms entities into choice keys * * @param Collection|object A collection of entities, a single entity or * NULL * @return mixed An array of choice keys, a single key or * NULL */ protected function transform($collectionOrEntity) { if (null === $collectionOrEntity) { return $this->getOption('multiple') ? array() : ''; } if (count($this->identifier) > 1) { // load all choices $availableEntities = $this->getEntities(); if ($collectionOrEntity instanceof Collection) { $result = array(); foreach ($collectionOrEntity as $entity) { // identify choices by their collection key $key = array_search($entity, $availableEntities); $result[] = $key; } } else { $result = array_search($collectionOrEntity, $availableEntities); } } else { if ($collectionOrEntity instanceof Collection) { $result = array(); foreach ($collectionOrEntity as $entity) { $result[] = current($this->getIdentifierValues($entity)); } } else { $result = current($this->getIdentifierValues($collectionOrEntity)); } } return parent::transform($result); }