setValue() public method

Sets the value of the field.
public setValue ( string $value )
$value string The value of the field
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');
     }
 }
Ejemplo n.º 2
0
 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.');
 }