Pop\Form\Form::setField PHP Method

setField() public method

Set a single field in $initFieldsValues
public setField ( string $name, array $field ) : Form
$name string
$field array
return Form
    public function setField($name, array $field)
    {
        $match = false;
        if (array_key_exists($name, $this->initFieldsValues)) {
            $this->initFieldsValues[$name] = $field;
            $match = true;
        } else {
            foreach ($this->initFieldsValues as $key => $value) {
                if (array_key_exists($name, $value)) {
                    $this->initFieldsValues[$key][$name] = $field;
                    $match = true;
                }
            }
        }
        if (!$match) {
            $keys = array_keys($this->initFieldsValues);
            if (is_numeric($keys[0])) {
                $last = $keys[count($keys) - 1];
                $this->initFieldsValues[$last][$name] = $field;
            } else {
                $this->initFieldsValues[$name] = $field;
            }
        }
        return $this;
    }

Usage Example

Example #1
0
 public function testGetAndSetField()
 {
     $fields = array('username' => array('type' => 'text', 'value' => 'Username here...', 'label' => 'Username:'******'required' => true, 'attributes' => array('size' => 40)), 'submit' => array('type' => 'submit', 'value' => 'SUBMIT'));
     $f = new Form('/submit', 'post', $fields);
     $submit = $f->getField('submit');
     $this->assertEquals('SUBMIT', $submit['value']);
     $submit['value'] = 'NEW SUBMIT';
     $f->setField('submit', $submit);
     $submit = $f->getField('submit');
     $this->assertEquals('NEW SUBMIT', $submit['value']);
 }