SimpleForm::setField PHP Method

setField() public method

Sets a widget value within the form.
public setField ( SelectorInterface $selector, string $value, $position = false ) : boolean
$selector SelectorInterface Criteria to apply.
$value string Value to input into the widget.
return boolean True if value is legal, false otherwise. If the field is not present, nothing will be set.
    public function setField(SelectorInterface $selector, $value, $position = false)
    {
        $success = false;
        $_position = 0;
        for ($i = 0, $count = count($this->widgets); $i < $count; $i++) {
            if ($selector->isMatch($this->widgets[$i])) {
                $_position++;
                if ($position === false or $_position === (int) $position) {
                    if ($this->widgets[$i]->setValue($value)) {
                        $success = true;
                    }
                }
            }
        }
        return $success;
    }

Usage Example

コード例 #1
0
 function testMultipleFieldsWithSameKey()
 {
     $form = new SimpleForm(new SimpleFormTag(array()), new SimpleUrl('htp://host'));
     $form->addWidget(new SimpleCheckboxTag(array('name' => 'a', 'type' => 'checkbox', 'value' => 'me')));
     $form->addWidget(new SimpleCheckboxTag(array('name' => 'a', 'type' => 'checkbox', 'value' => 'you')));
     $this->assertIdentical($form->getValue(new SimpleByName('a')), false);
     $this->assertTrue($form->setField(new SimpleByName('a'), 'me'));
     $this->assertIdentical($form->getValue(new SimpleByName('a')), 'me');
 }