Symfony\Component\DomCrawler\Field\ChoiceFormField::setValue PHP Method

setValue() public method

Sets the value of the field.
public setValue ( string $value )
$value string The value of the field
    public function setValue($value)
    {
        if ('checkbox' === $this->type && false === $value) {
            // uncheck
            $this->value = null;
        } elseif ('checkbox' === $this->type && true === $value) {
            // check
            $this->value = $this->options[0]['value'];
        } else {
            if (is_array($value)) {
                if (!$this->multiple) {
                    throw new \InvalidArgumentException(sprintf('The value for "%s" cannot be an array.', $this->name));
                }
                foreach ($value as $v) {
                    if (!$this->containsOption($v, $this->options)) {
                        throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: %s).', $this->name, $v, implode(', ', $this->availableOptionValues())));
                    }
                }
            } elseif (!$this->containsOption($value, $this->options)) {
                throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: %s).', $this->name, $value, implode(', ', $this->availableOptionValues())));
            }
            if ($this->multiple) {
                $value = (array) $value;
            }
            if (is_array($value)) {
                $this->value = $value;
            } else {
                parent::setValue($value);
            }
        }
    }

Usage Example

Ejemplo n.º 1
0
 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');
     }
 }
All Usage Examples Of Symfony\Component\DomCrawler\Field\ChoiceFormField::setValue