Returns true if the field accepts multiple values.
public isMultiple ( ) : boolean | ||
Результат | boolean | true if the field accepts multiple values, false otherwise |
public function testCheckboxes() { $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name')); $field = new ChoiceFormField($node); $this->assertFalse($field->hasValue(), '->hasValue() returns false when the checkbox is not checked'); $this->assertNull($field->getValue(), '->getValue() returns null if the checkbox is not checked'); $this->assertFalse($field->isMultiple(), '->hasValue() returns false for checkboxes'); try { $field->addChoice(new \DOMNode()); $this->fail('->addChoice() throws a \\LogicException for checkboxes'); } catch (\LogicException $e) { $this->assertTrue(true, '->initialize() throws a \\LogicException for checkboxes'); } $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked')); $field = new ChoiceFormField($node); $this->assertTrue($field->hasValue(), '->hasValue() returns true when the checkbox is checked'); $this->assertEquals('1', $field->getValue(), '->getValue() returns 1 if the checkbox is checked and has no value attribute'); $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo')); $field = new ChoiceFormField($node); $this->assertEquals('foo', $field->getValue(), '->getValue() returns the value attribute if the checkbox is checked'); $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo')); $field = new ChoiceFormField($node); $field->setValue(false); $this->assertNull($field->getValue(), '->setValue() unchecks the checkbox is value is false'); $field->setValue(true); $this->assertEquals('foo', $field->getValue(), '->setValue() checks the checkbox is value is true'); try { $field->setValue('bar'); $this->fail('->setValue() throws an \\InvalidArgumentException if the value is not one from the value attribute'); } catch (\InvalidArgumentException $e) { $this->assertTrue(true, '->setValue() throws an \\InvalidArgumentException if the value is not one from the value attribute'); } }
/** * @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"); }