It is constructed from a HTML select tag, or a HTML checkbox, or radio inputs.
Author: Fabien Potencier (fabien@symfony.com)
Inheritance: extends FormField
Example #1
0
 public function testOptionWithNoValue()
 {
     $node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => false));
     $field = new ChoiceFormField($node);
     $field->select('foo');
     $this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
 }
Example #2
0
 /**
  * @param Crawler $nodes
  * @return array|mixed|string
  */
 protected function proceedGetValueFromField(Crawler $nodes)
 {
     $values = [];
     if ($nodes->filter('textarea')->count()) {
         return (new TextareaFormField($nodes->filter('textarea')->getNode(0)))->getValue();
     }
     if ($nodes->filter('input')->count()) {
         $input = $nodes->filter('input');
         if ($input->attr('type') == 'checkbox' or $input->attr('type') == 'radio') {
             $values = [];
             $input = $nodes->filter('input:checked');
             foreach ($input as $checkbox) {
                 $values[] = $checkbox->getAttribute('value');
             }
             return $values;
         }
         return (new InputFormField($nodes->filter('input')->getNode(0)))->getValue();
     }
     if ($nodes->filter('select')->count()) {
         $field = new ChoiceFormField($nodes->filter('select')->getNode(0));
         $options = $nodes->filter('option[selected]');
         foreach ($options as $option) {
             $values[] = $option->getAttribute('value');
         }
         if (!$field->isMultiple()) {
             return reset($values);
         }
         return $values;
     }
     $this->fail("Element {$nodes} is not a form field or does not contain a form field");
 }
Example #3
0
 public function testSelect()
 {
     $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
     $field = new ChoiceFormField($node);
     $field->select(true);
     $this->assertEquals(1, $field->getValue(), '->select() changes the value of the field');
     $field->select(false);
     $this->assertEquals(null, $field->getValue(), '->select() changes the value of the field');
     $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
     $field = new ChoiceFormField($node);
     $field->select('foo');
     $this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
 }
 public function testDisableValidation()
 {
     $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
     $field = new ChoiceFormField($node);
     $field->disableValidation();
     $field->setValue('foobar');
     $this->assertEquals('foobar', $field->getValue(), '->disableValidation() allows to set a value which is not in the selected options.');
     $node = $this->createSelectNode(array('foo' => false, 'bar' => false), array('multiple' => 'multiple'));
     $field = new ChoiceFormField($node);
     $field->disableValidation();
     $field->setValue(array('foobar'));
     $this->assertEquals(array('foobar'), $field->getValue(), '->disableValidation() allows to set a value which is not in the selected options.');
 }
 public function testSelectWithEmptyValue()
 {
     $node = $this->createSelectNodeWithEmptyOption(array('' => true, 'Female' => false, 'Male' => false));
     $field = new ChoiceFormField($node);
     $this->assertSame('', $field->getValue());
 }